1

在我的警报视图中,有两个按钮,确定和取消。当用户单击“确定”按钮时,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

不幸的是,目前这是不可能的(iOS 5.1)。AlertView 类不支持块。

于 2012-06-08T06:03:59.673 回答
1

我制作了一对UIAlertViewUIActionSheet子类来做到这一点。在这里抓住它们: 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 回答