iphone - 在 AlertView OK 按钮声明中执行一段代码

标签 iphone objective-c ios uialertview

在我的警报 View 中,有两个按钮,确定和取消。当用户点击确定按钮时,委托(delegate)方法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 

委托(delegate)方式:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{

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

-(void)aMethod{

//Some useful code
}

现在,我想要的不是所有这些,而是​​执行 aMethod 的代码。直接在 AlertView 中的方法,而不引用委托(delegate)方法和被调用的方法,类似这样:
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]; 

是否可以?

最佳答案

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

关于iphone - 在 AlertView OK 按钮声明中执行一段代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10943747/

相关文章:

ios - 而不是 push segue 如何替换 View Controller (或从导航堆栈中删除)?

iphone - AVAudioPlayer - 在 UILabel 上显示倒数计时器

iphone - 突破多个循环

iphone - TouchXML 命名空间解析?

iphone - 返回时保留?

iphone - "setting"UI 是由 iphone/touch/ipad 中的表格 View 实现的吗?

ios - 未指定基类而定义的类。找不到 UIViewController 的接口(interface)声明

ios - UIImageView 动画在 UICollectionViewCell 中不能正常工作

iphone - Objective-C 宏重新定义

ios - 是否可以在一个 UiTableView 中有两个不同的 UiTableViewCell?