【问题标题】:Adding multiple UIButtons to an UIView with a selector action使用选择器操作向 UIView 添加多个 UIButton
【发布时间】:2018-02-22 16:42:04
【问题描述】:

我以编程方式向 UIView(通过 addSubview)添加了一些按钮。我在这个按钮上添加了一个带有函数的@selector。但是,它们出现在视图中,但是当我单击时,最后一个按钮仅起作用。

进入我的 .h:

@property (nonatomic, strong) UIButton * myButton;

进入我的.m

for(int i=0;i<5;i++){

myButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(55, 55*i, 30, 30);
myButton.tag = i;
myButton.backgroundColor = [UIColor redColor];
[myButton addTarget:self action:@selector(myaction:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:myButton];

}

-(void)myaction:(UIButton *)sender{
  if(sender.tag == 0){
    NSLog(@“uibutton clicked %ld", (long)sender.tag);
  }
}

如何将动作添加到所有按钮?不只是最后一个......

【问题讨论】:

  • 你每次都覆盖同一个变量?
  • 那你的问题是什么?看到全部 5 个按钮了吗?是否为所有 5 个按钮调用了 myaction?它应该基于您发布的代码。
  • 我可以看到所有 5 个按钮,但只有当我单击 5 号按钮时才会调用它。当我点击其他按钮时不调用
  • 发生的事情一定比你展示的要多。正如 Larme 所提到的,一个明显的困惑是您对 myButton 属性的奇怪使用。为什么要使用一个按钮属性来创建 5 个按钮?只需使用局部变量并摆脱该属性即可。
  • 显示整个代码,肯定有东西干扰了按钮

标签: ios objective-c uibutton uicontrolevents addtarget


【解决方案1】:

这很好用:

- (void)viewDidLoad {
    [super viewDidLoad];

    for(int i=0;i<5;i++){

        UIButton *myButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
        myButton.frame = CGRectMake(55, 55*i, 30, 30);
        myButton.tag = i;
        myButton.backgroundColor = [UIColor redColor];
        [myButton setTitle:[NSString stringWithFormat:@"%ld", (long)i] forState:UIControlStateNormal];
        [myButton addTarget:self action:@selector(myaction:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:myButton];
    }


}

-(void)myaction:(UIButton *)sender{
    NSLog(@"uibutton clicked %ld", (long)sender.tag);
}

您在问题中发布的代码似乎是“这就是我的代码的样子”,而不是您的实际代码。当人们试图提供帮助时,这可能会导致问题。

以后,发布您的实际代码。

【讨论】:

  • 这是问题中的相同代码(按钮标题除外)。这如何解决问题?
【解决方案2】:

让我们通过计算按钮的高度并根据按钮的数量添加填充来使其更具动态性。

-(void)viewDidLoad {

 [super viewDidLoad];

 NSInteger height = self.frame.size.height - 5*20; // 5 is the button count 
 and 20 is the padding 
 NSInteger buttonHeight = height/5;
 for(int i=0;i<5;i++){
    UIButton *myButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
    myButton.frame = CGRectMake(55, buttonHeight*i+padding*(i+1), 150, 
     buttonHeight);
    myButton.tag = i;
    myButton.backgroundColor = [UIColor redColor];
    [myButton setTitle:[NSString stringWithFormat:@"%ld", (long)i] 
    forState:UIControlStateNormal];
    [myButton addTarget:self action:@selector(myaction:) 
    forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:myButton];
}}

-(void)myaction:(UIButton *)sender{
 NSLog(@"uibutton clicked %ld", (long)sender.tag);
}

您可以通过像这样计算按钮的高度来使其更具动态性:

NSInteger height = self.frame.size.height - 5*20; 
NSInteger buttonHeight = height/5;

【讨论】:

  • 请不要在没有任何解释的情况下发布代码。一个好的答案可以解释问题所在以及答案如何解决问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-10
  • 2015-02-11
  • 1970-01-01
  • 2010-12-17
  • 1970-01-01
相关资源
最近更新 更多