我正在动态创建三个按钮和一个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];

   }

最佳答案

您的编码很好,但是您没有为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];

}


输出是

10-04 14:00