0

私はこの2ボタンのアラートビューを持っています:

UIAlertView* message = [[UIAlertView alloc]
                           initWithTitle: @"Delete?" message: @"This business will be deleted permenently." delegate: nil
                           cancelButtonTitle: @"Cancel" otherButtonTitles: @"Delete", nil];

[message show];

私もこの方法を持っています:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"Delete"])
    {
        NSLog(@"Button DELETE was selected.");
    }
    else if([title isEqualToString:@"Cancel"])
    {
        NSLog(@"Button CANCEL was selected.");
    }
}

これを.hファイルに追加しました。

<UIAlertViewDelegate>

現在、いずれかのボタンを押すと、ダイアログが閉じます。キャンセルしても大丈夫ですが、削除ボタンが押されたことをどうやって知ることができますか?

ありがとう!

4

3 に答える 3

5

– alertView:clickedButtonAtIndex:UIAlertViewDelegateのメソッドを実装する必要があります。また、アラートビューを初期化するときにデリゲートを設定する必要があります。

例えば

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

   if (buttonIndex == 0) {
        //Do something
   } else if (buttonIndex == 1) {
       //Do something else
   }
}

キャンセルボタンのインデックスは0です。

于 2012-11-28T17:42:36.460 に答える
3

アラートビューを作成するときnilに、パラメータに渡します。代わりdelegateに合格する必要があります。selfあなたが今それを持っているように、clickedButtonAtIndex:メソッドは決して呼び出されません。

UIAlertView* message = [[UIAlertView alloc]
    initWithTitle: @"Delete?" 
    message: @"This business will be deleted permenently." 
    delegate: self
    cancelButtonTitle: @"Cancel" 
    otherButtonTitles: @"Delete", nil];


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == alertView.cancelButtonIndex) {
        // Cancel was tapped
    } else if (buttonIndex == alertView.firstOtherButtonIndex) {
        // The other button was tapped
    }
}
于 2012-11-28T17:57:47.197 に答える
2
message.delegate = self;
...

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
     NSLog(@"Button %d was clicked", buttonIndex);
}

UIAlertViewDelegateまた、プロトコルを満たすためにクラスを宣言する必要があります。

于 2012-11-28T17:46:04.280 に答える