UIAlertController在外部点击时处理解除(IPad) [英] UIAlertController handle dismiss upon click outside (IPad)

查看:510
本文介绍了UIAlertController在外部点击时处理解除(IPad)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS8之前,我们使用UIActionSheet显示警告,现在我们需要使用UIAlertController。

Previous to iOS8 we used the UIActionSheet for showing alert and now we need to use the UIAlertController.

当我们使用UIActionSheet时,我们可以轻松处理用户的情况通过将clickedButtonAtIndex与cancelButtonIndex进行比较来点击弹出窗口外部(这意味着他想要取消操作) - 如果用户确实在弹出窗口外按下了我们在此函数中获得了取消按钮索引。

When we used the UIActionSheet we could easily handle situations where the user clicked outside the pop up (which means he want to cancel the operation) by comparing the clickedButtonAtIndex to the cancelButtonIndex - if the user indeed pressed outside the popup we got the cancel button index in this function.

我们如何使用新的UIAlertController处理这些情况?我试图使用完成块,但它没有任何上下文。有一个简单的方法来处理这个? (除了保存某些常规变量中的动作状态)。

How can we handle these situations with the new UIAlertController? I tried to use the "completion" block but it doesn't have any context. Is there an easy way to handle this? (other than "saving" the actions states in some general variable).

推荐答案

您可以使用样式添加动作:UIAlertActionStyleCancel和当用户点击弹出窗口时,会调用此操作的处理程序。

You can add an action with style:UIAlertActionStyleCancel and the handler for this action is called when the user taps outside the popup.

if ([UIAlertController class]) {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert Title" message:@"A Message" preferredStyle:UIAlertControllerStyleActionSheet];

    [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"User clicked button called %@ or tapped elsewhere",action.title);
    }]];

    [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"User clicked button called %@",action.title);
    }]];

    [alertController addAction:[UIAlertAction actionWithTitle:@"Other" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        NSLog(@"User clicked button called %@",action.title);
    }]];

    UIControl *aControl = (UIControl *) sender;
    CGRect frameInView = [aControl convertRect:aControl.bounds toView:self.view];
    alertController.popoverPresentationController.sourceRect = frameInView;
    alertController.popoverPresentationController.sourceView = self.view;
    alertController.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
    [self presentViewController:alertController animated:YES completion:nil];
}

这篇关于UIAlertController在外部点击时处理解除(IPad)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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