UIAlertAction完成块未调用-iOS [英] UIAlertAction completion block not called - iOS

查看:63
本文介绍了UIAlertAction完成块未调用-iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有UIAlertController的iOS应用,当用户点击按钮时,该应用以操作表的形式显示在我的应用中.

I have an iOS app with a UIAlertController in the form of a action sheet being presented in my app when the user taps on a button.

这一切都很好,除了一件事之外,由于某些原因而无法调用完成区块.

It all works great, apart from one thing, the completion blocks don't get called for some reason.

这是我的代码:

// 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,则它会运行....为什么?

If you run that code you will see the line which dismisses the controller wont run and neither will the NSLog statement inside it. However, if you delete the NSLog and set the completion block to nil, then it does run.... why???

谢谢您的时间,丹.

推荐答案

不要尝试关闭警报控制器.到您的警报操作处理程序被调用时,它将为您取消.

Don't attempt to dismiss the alert controller. It will be dismissed for you by the time your alert action's handler is called.

将取消"操作更改为:

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
    NSLog(@"asdhfgjk");
}];

这篇关于UIAlertAction完成块未调用-iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆