我的按钮和textview没有显示在iOS的屏幕上?

时间:2014-10-29 07:45:03

标签: ios dynamic uiview xcode6

我正在动态创建三个按钮和一个uitextview但是当我运行程序时它不会显示在我的屏幕上

这是我创建动态按钮和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];

   }

2 个答案:

答案 0 :(得分:1)

您正在将self.view分配给另一个尚未添加到viewController的UIView实例。请注意,self.view已经在viewController中初始化,重新分配它是危险的。

此外,您尚未为按钮和文本视图设置任何帧。

以下代码应该可以使用。

-(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];

   }

答案 1 :(得分:1)

您的编码很好,但是您没有添加UIButtonUItextview的框架

-(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