【问题标题】:Multiple actions for the one BarButtonItem一个 BarButtonItem 的多个操作
【发布时间】:2014-03-26 11:37:04
【问题描述】:

我正在尝试将两个操作分配给一个 BarButtonItem,但我的语法 (bad receiver type 'NSInteger' aka 'long') 有问题,我无法编译我的应用程序。这是错误的代码:

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] 
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(myAction1)];
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(myAction2)];

我正在使用Xcode 5.1,我的目标是iOS 7.0.

你能帮帮我吗?提前谢谢你。

【问题讨论】:

  • 此方法不存在。调用将调用Action1Action2 的方法
  • 你想用两种方法做什么?
  • 为什么要调用 2 个动作?它根本不可能。您可以将第二个动作作为第一个动作中的方法调用。或使用 nsnotifications 通知任何已注册通知的人
  • 这些答案对您有帮助吗?
  • 嗨,JoeFryer,对我有帮助的答案如下所示。

标签: ios uinavigationcontroller uibarbuttonitem


【解决方案1】:

您不能将两个操作分配给 BarButtonItem

您可以在selector 方法中调用第二个操作。

或者您可以简单地删除以前的目标并在运行时有条件地添加新的target action

【讨论】:

    【解决方案2】:

    您不能向UIBarButtonItem 添加多个目标/操作。

    您可以像这样添加一个带有一个目标/动作的条形按钮项:

    UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(myAction1)];
    self.navigationItem.leftBarButtonItem = myBarButtonItem;
    

    然后你需要做一些事情,比如从指定的方法中调用第二种方法,或者任何适合你情况的方法。

    【讨论】:

      【解决方案3】:

      这真的是你的代码吗?

      initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(myAction2)];
      

      接收器不是不见了吗?

      UIBarButtonItem 也只能有一个目标,如果您想要调用两个动作,请创建第三个调用另外两个的动作

      - (IBAction)myAction3:(id)sender {
        [self myAction1:sender];
        [self myAction2:sender];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-03
        • 1970-01-01
        • 2012-03-19
        • 2018-11-20
        • 2010-11-05
        相关资源
        最近更新 更多