2014-10-29 129 views
0

我创建三个按钮和一个UITextView的动态,但是当我运行程序它不是我的屏幕我的按钮和textview不显示在iOS屏幕上?

这是我的代码来创建动态按钮及TextView的

-(void)getSmsdisplay 

{ 
    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] 
    applicationFrame]]; 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    UITextView *textview=[[UITextView alloc]init]; 
    [button setTitle:@"Comment" forState:UIControlStateNormal]; 
    [button1 setTitle:@"Share" forState:UIControlStateNormal]; 
    [button1 setTitle:@"Like" forState:UIControlStateNormal]; 

    //listen for clicks 
    [button addTarget:self 
    action:@selector(btncomment:)forControlEvents:UIControlEventTouchUpInside]; 
    [button1 addTarget:self 
    action:@selector(btnShare:)forControlEvents:UIControlEventTouchUpInside]; 

    [button2 addTarget:self 
    action:@selector(Like:)forControlEvents:UIControlEventTouchUpInside]; 

    smsdata *data=[[smsdata alloc]init]; 

    [textview setText:data.Data]; 
    [self.view addSubview:textview]; 



    //add the button to the view 
    [self.view addSubview:button]; 
    [self.view addSubview:button1]; 
    [self.view addSubview:button2]; 

    } 
+0

您重新分配self.view到另一个,这是不添加任何其他子视图。 – ZeMoon 2014-10-29 07:48:19

回答

1

您的编码是好​​的,但乌尔不添加UIButton框架和UItextview

-(void)getSmsdisplay 

{ 
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] 
              applicationFrame]]; 
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 


UITextView *textview=[[UITextView alloc]init]; 

button.frame=CGRectMake(20, 50, 200, 50); 

    button1.frame=CGRectMake(20, 150, 200, 50); 

    button2.frame=CGRectMake(20, 300, 200, 50); 

button.backgroundColor=[UIColor redColor]; 
    button1.backgroundColor=[UIColor grayColor]; 
    button2.backgroundColor=[UIColor blackColor]; 

[button setTitle:@"Comment" forState:UIControlStateNormal]; 
[button1 setTitle:@"Share" forState:UIControlStateNormal]; 
[button2 setTitle:@"Like" forState:UIControlStateNormal]; 

//listen for clicks 
[button addTarget:self 
      action:@selector(btncomment:)forControlEvents:UIControlEventTouchUpInside]; 
[button1 addTarget:self 
      action:@selector(btnShare:)forControlEvents:UIControlEventTouchUpInside]; 

[button2 addTarget:self 
      action:@selector(Like:)forControlEvents:UIControlEventTouchUpInside]; 



[textview setText:@"karthik"]; 
[self.view addSubview:textview]; 



//add the button to the view 
[self.view addSubview:button]; 
[self.view addSubview:button1]; 
[self.view addSubview:button2]; 

} 

输出enter image description here

+0

我想这个按钮在我的窗口底部像水平 – 2014-10-29 08:02:06

+0

只是改变框架, – 2014-10-29 08:46:07

+0

意味着我没有得到你 – 2014-10-29 09:25:08

1

你要分配self.view上显示另一个UIView实例,它没有被添加到viewController。请注意,self.view已经在viewController中初始化了,重新分配它是很危险的。

此外,您还没有设置按钮和textview的任何框架。

下面的代码应该使它工作。

-(void)getSmsdisplay 

{ 
    UIView *smsView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] 
    applicationFrame]]; 
    [self.view addSubview: smsView]; //The new view needs to be added to something! 

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    UITextView *textview=[[UITextView alloc]initWithFrame:CGRectMake:(20,20,100,50)]; 
    [button setTitle:@"Comment" forState:UIControlStateNormal]; 
    [button1 setTitle:@"Share" forState:UIControlStateNormal]; 
    [button1 setTitle:@"Like" forState:UIControlStateNormal]; 

    //listen for clicks 
    [button addTarget:self 
    action:@selector(btncomment:)forControlEvents:UIControlEventTouchUpInside]; 
    [button1 addTarget:self 
    action:@selector(btnShare:)forControlEvents:UIControlEventTouchUpInside]; 

    [button2 addTarget:self 
    action:@selector(Like:)forControlEvents:UIControlEventTouchUpInside]; 

    smsdata *data=[[smsdata alloc]init]; 

    [textview setText:data.Data]; 
    [smsView addSubview:textview]; 

    button.frame=CGRectMake(20, 50, 200, 50); 
    button1.frame=CGRectMake(20, 150, 200, 50); 
    button2.frame=CGRectMake(20, 300, 200, 50); 

    //add the button to the view 
    [smsView addSubview:button]; 
    [smsView addSubview:button1]; 
    [smsView addSubview:button2]; 

    } 
+0

tanx支持提问者,你的回答也很好 – 2014-10-29 09:25:45