【问题标题】:How can I dynamically create UIButtons with Cocoa Touch?如何使用 Cocoa Touch 动态创建 UIButton?
【发布时间】:2012-03-29 05:28:17
【问题描述】:

如何使用 Cocoa Touch 动态创建 UIButton?

【问题讨论】:

  • for(int i=0;i<10;i++) { UIButton*but=[UIButton buttonWithType: UIButtonTypeRoundedRect]; [self.view addSubview:but]; },这将创建按钮,但这应该很少研究就能找到,你面临什么问题?
  • 嘿@Vicky,请先搜索,然后发布问题....:)

标签: ios cocoa-touch


【解决方案1】:
 UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    myButton.frame = CGRectMake(20, 20, 200, 44); // position in the parent view and set the size of the button
    [myButton setTitle:@"Click Me!" forState:UIControlStateNormal];
    // add targets and actions
    [myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    // add to a view
    [superView addSubview:myButton];

【讨论】:

    【解决方案2】:
    int yOfs = 0;
    for (int index = 0; index<10; index++) {
        UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [aButton setBackgroundImage:[UIImage imageNamed:@"buttonImage"] forState:UIControlStateNormal];
        [aButton setTitle:[NSString stringWithFormat:@"Button %d",index] forState:UIControlStateNormal];
        [aButton setFrame:CGRectMake(20, yOfs, 100, 50)];
        [aButton addTarget:self action:@selector(aButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [aView addSubview:aButton];
    
        yOfs += 50;
    }
    
    
    - (IBAction)aButtonClicked:(id)sender
    {
        NSLog(@"Clicked on button with title %@",[sender titleLabel].text);
    }
    

    【讨论】:

    • 对于您的示例,最好将您的按钮存储在一个数组中
    【解决方案3】:
      UIImage *buttonImage = [UIImage imageNamed:@"tabbar - refresh.png"];
        
        refresh = [UIButton buttonWithType:UIButtonTypeCustom];
        refresh.frame = CGRectMake(frame values);
        [refresh setBackgroundImage:buttonImage forState:UIControlStateNormal];
        [refresh addTarget:self action:@selector(refreshPressed:) forControlEvents:UIControlEventTouchUpInside];    
        [self.view addSubview:refresh];
    

    【讨论】:

      【解决方案4】:
      UIButton *btnDone;
      btnDone = [UIButton buttonWithType:UIButtonTypeCustom];
      [btnDone setFrame:CGRectMake(0, 0, 28, 29)];
      
      // For setting an image to button....
      [btnDone setImage:[UIImage imageNamed:@"done.png"] forState:UIControlStateNormal];
      [btnDone setImage:[UIImage imageNamed:@"done_hover.png"] forState:UIControlStateHighlighted];
      
      // Add target to button...
      [btnDone addTarget:self action:@selector(btnDoneAction:) forControlEvents:UIControlEventTouchUpInside];
      
      //Method implementation..
      -(void)btnDoneAction:(id)sender
      {
          //Your stuff..
      }
      

      【讨论】:

        猜你喜欢
        • 2011-07-24
        • 2010-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-31
        • 1970-01-01
        • 2016-02-29
        相关资源
        最近更新 更多