【问题标题】:UIBarButtonItem action fails with unrecognised selectorUIBarButtonItem 操作失败,选择器无法识别
【发布时间】:2013-05-07 09:16:37
【问题描述】:

我搜索了很多,但找不到解决问题的方法。我有一个函数,当点击 UIBarButtonItem 之一时,我想将其作为选择器调用。我的 UIViewController 嵌入在导航控制器中。

在 .h 文件中

- (IBAction)EventsAction:(UIBarButtonItem *)sender;

在我的 .m 文件中,我在 viewDidLoad 方法中创建 UIBarButtonItems

UIBarButtonItem *composer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(EventActions:)];
composer.tag = 0;

UIBarButtonItem *notifList = [[UIBarButtonItem alloc] initWithTitle:@"!N!" style:UIBarButtonItemStyleBordered target:self action:@selector(EventActions:)];
notifList.tag = 1;

NSArray *buttonArr = [[NSArray alloc] initWithObjects:composer, notifList, nil];
self.navigationItem.rightBarButtonItems = buttonArr;

而函数定义为:

- (IBAction)EventsAction:(UIBarButtonItem *)sender {

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"connection"]) {
    if (sender.tag == 0) {
        [self performSegueWithIdentifier:@"newEvent" sender:self];
    }
    if (sender.tag == 1) {
        [self performSegueWithIdentifier:@"notificationView" sender:self];
    }
}

else {
    UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"NoConnectionTitle", nil) message:NSLocalizedString(@"NoConnection", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"Tamam", nil) otherButtonTitles:nil];
    [myAlert show];
}

}

但是,当我运行此代码并点击其中一个按钮时,出现无法识别的选择器错误

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[BIAllEventsController EventActions:]:无法识别的选择器发送到实例 0x1ddc1400”

我也尝试将 IBAction 更改为 void,但也没有解决我的问题。

编辑

谢谢大家,对这个愚蠢的错误深表歉意。尽管它教授了非常重要的课程,例如:

  • 永远不要相信自动完成
  • 睡个好觉并不能解决所有问题

【问题讨论】:

  • 这是因为选择器EventActions: 无法识别。仔细看看它的名字。

标签: objective-c xcode uibarbuttonitem unrecognized-selector


【解决方案1】:

您的代码中有一个拼写错误,您的方法名称是 EventsAction,带有 s,但在这一行

UIBarButtonItem *composer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(EventActions:)];

您使用的EventActions 没有s。您必须更改UIBarButtonItems 的初始化方法声明。

【讨论】:

    【解决方案2】:

    因为您的方法名称是 EventsAction,但您要添加 EventActions(s 错位)作为 barButton 的选择器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多