私はたくさん検索しましたが、私の問題の解決策を見つけることができませんでした。UIBarButtonItem の 1 つがタップされたときにセレクターとして呼び出したい関数があります。私の UIViewController は Navigation Controller に埋め込まれています。
.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];
}
}
ただし、このコードを実行してボタンの 1 つをタップすると、認識できないセレクタ エラーが発生します。
キャッチされていない例外 'NSInvalidArgumentException' が原因でアプリを終了しています。理由: '-[BIAllEventsController EventActions:]: 認識されないセレクターがインスタンス 0x1ddc1400 に送信されました'
IBAction を void に変更しようとしましたが、問題も解決しませんでした。
編集
皆さんありがとうございます。愚かな間違いをお詫び申し上げます。それは次のような非常に重要な教訓を教えていますが:
- オートコンプリートを信用しない
- ぐっすり眠ればすべての問題が解決するわけではない