2014-10-29 127 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