在我的警报 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/