1

私のアラート ビューには、[OK] と [キャンセル] の 2 つのボタンがあります。ユーザーが [OK] ボタンをクリックすると、デリゲート メソッドdismissWithClickedButtonIndex:animatedが呼び出され、インデックスが 0 の場合、コードを実行するメソッドが呼び出されます。

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" 
                                                    message:@"Are you sure you want to exit"
                                                   delegate:self cancelButtonTitle: @"OK" 

                                          otherButtonTitles: @"Cancel",nil]; 


    [alert show]; 
    [alert release];//release the reference 

委任方法:

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

    if (buttonIndex==0) {
        [self aMethod];
    }    
}

-(void)aMethod{

//Some useful code
}

今、私がしたいのは、これらすべての代わりに、aMethodデリゲートメソッドと呼び出されるメソッドを参照せずに、AlertView でメソッドのコードを直接実行することです。

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" 
                                                message:@"Are you sure you want to exit"
                                               delegate:self cancelButtonTitle: @"OK" //Put here some useful code

                                      otherButtonTitles: @"Cancel",nil]; 

出来ますか?

4

2 に答える 2

2

残念ながら、現時点ではこれは不可能です(iOS5.1)。AlertViewクラスはブロックをサポートしていません。

于 2012-06-08T06:03:59.673 に答える
1

UIAlertViewまさにそれを行うとUIActionSheetサブクラスのペアを作成しました。ここから入手してください: https://github.com/rydermackay/RMActionSheet

次のように使用します。

RMAlertView *alertView = [RMAlertView alertViewWithTitle:@"Alert!" message:nil];

[alertView addButtonWithTitle:@"OK"
                       action:^{
                           NSLog(@"OK");
                       }];

[alertView addCancelButtonWithTitle:@"Cancel"
                             action:nil];
[alertView show];

編集:

あなたのコメントから、あなたはブロックに慣れていないようです。今すぐこれを読んでください。真剣に。 http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/00_Introduction.html

これも良いものです: http://www.mikeash.com/pyblog/friday-qa-2009-08-14-practical-blocks.html

于 2012-06-08T06:22:43.307 に答える