【问题标题】:Add target for UIButton - iOS [closed]为 UIButton 添加目标 - iOS [关闭]
【发布时间】:2012-08-13 18:03:37
【问题描述】:

任何人都可以帮助我添加操作以使用UIButton 调用以下方法。

-(void)navigatePics:(id)sender andIndex:(NSInteger *)index{}

【问题讨论】:

  • 请说明您想要什么类型的功能。您是否希望有人在 {} 之间写入以使该方法执行某些操作?或者您需要帮助将其连接到 Interface Builder (IB)?
  • 我只是在寻找 [button addTarget:self action:@selector(_this space____) forControlEvents:UIControlEventTouchUpInside];

标签: ios uibutton


【解决方案1】:

使用 button.setTag:(NSInteger) 方法将索引作为标签添加到 UIButton。

UIButton *button = ...;
[button setTag:1234];
[button addTarget:self action:@selector(navigatePics:) forControlEvents:UIControlEventTouchUpInside];

然后在 navigatePics 中读取标签。

-(void)navigatePics:(UIButton *)sender
{
 int index = sender.tag;
 // index = 1234;
}

【讨论】:

    【解决方案2】:
    UIButton *button = [[UIButton alloc] init];
    button.tag = 4; //index
    [button addTarget:self @selected(buttonDidTap:)];
    
    ...
    [button release];
    
    -(void)buttonDidTap:(UIButton *)sender{
    NSInteger index = sender.tag;
    }
    

    【讨论】:

      【解决方案3】:

      这应该可以解决问题。如果您想在另一个实例上调用此方法,请不要使用 self。在这种情况下,只需使用该实例代替 self。

      [button addTarget:self action:@selector(navigatePics:andIndex:) forControlEvents:UIControlEventTouchUpInside];
      

      【讨论】:

      • 如何传递索引值?
      • 无法传递索引值...从发件人处获取
      猜你喜欢
      • 2014-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-13
      • 1970-01-01
      相关资源
      最近更新 更多