当用户点击按钮时,我的应用程序中会显示一个带有UIAlertController的iOS应用程序 .
这一切都很好,除了一件事,完成块不会因某种原因被调用 .
这是我的代码:
// Setup the alert information/type.
UIAlertController *action_view = [UIAlertController alertControllerWithTitle:@"Test title" message:@"test message" preferredStyle:UIAlertControllerStyleActionSheet];
// Create and add the cancel button.
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
[action_view dismissViewControllerAnimated:YES completion:^{
NSLog(@"asdhfgjk");
}];
}];
// Add the action button to the alert.
[action_view addAction:cancel];
// Present the alert to the user.
[self presentViewController:action_view animated:YES completion:nil];
如果您运行该代码,您将看到解除控制器不会运行的行,并且它内部的 NSLog
语句也不会 . 但是,如果删除 NSLog
并将完成块设置为nil,那么它确实运行....为什么???
谢谢你的时间,丹 .
2 回答
请勿尝试关闭警报控制器 . 在调用警报操作的处理程序时,它将被解雇 .
将“取消”操作更改为:
“取消”操作不应该忽略rmaddy提到的视图控制器 . 但是,即使“取消”操作设置为:
您可能会看到未调用完成块的相同问题 . 例如,实现这个(有点人为的)方法:
也可能有这种效果,因为UIAlertController在调用完成块之前被解雇 .