0

私はたくさん検索しましたが、私の問題の解決策を見つけることができませんでした。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 に変更しようとしましたが、問題も解決しませんでした。

編集

皆さんありがとうございます。愚かな間違いをお詫び申し上げます。それは次のような非常に重要な教訓を教えていますが:

  • オートコンプリートを信用しない
  • ぐっすり眠ればすべての問題が解決するわけではない
4

2 に答える 2

1

コードにスペルミスがあります。メソッドの名前にはsEventsActionが付いていますが、この行では

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

sEventActionsなしで使用しています。s の初期化のメソッド宣言を変更する必要があります。UIBarButtonItem

于 2013-05-07T09:19:53.130 に答える