Ios UIAlertController手柄在单击外部时关闭(IPad)

Ios UIAlertController手柄在单击外部时关闭(IPad),ios,ipad,ios8,uiactionsheet,uialertcontroller,Ios,Ipad,Ios8,Uiactionsheet,Uialertcontroller,在iOS8之前,我们使用UIActionSheet显示警报,现在我们需要使用UIAlertController 当我们使用UIActionSheet时,通过比较ClickedButtonIndex和cancelButtonIndex,我们可以轻松处理用户在弹出窗口外单击(这意味着他想要取消操作)的情况-如果用户确实在弹出窗口外按下,我们将在此函数中获得cancel按钮索引 我们如何使用新的UIAlertController处理这些情况?我试图使用“completion”块,但它没有任何上下文。有

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

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


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

您可以添加一个样式为UIAlertActionStyleCancel的操作,当用户点击弹出窗口外部时,将调用此操作的处理程序

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的解决方案。只需要将手势识别器添加到alertController superview

    [self presentViewController: alertController
                   animated: YES
                 completion:^{
                     alertController.view.superview.userInteractionEnabled = YES;
                     [alertController.view.superview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(alertControllerBackgroundTapped)]];
                 }];

- (void)alertControllerBackgroundTapped
{
    [self dismissViewControllerAnimated: YES
                             completion: nil];
}

UITapGestureRecognitor不适用于我,因此我使用了以下方法:

func addDismissControl(_ toView: UIView) {
    let dismissControl = UIControl()
    dismissControl.addTarget(self, action: #selector(self.dismissAlertController), for: .touchDown)
    dismissControl.frame = toView.superview?.frame ?? CGRect.zero
    toView.superview?.insertSubview(dismissControl, belowSubview: toView)
}

func dismissAlertController() {
    self.dismiss(animated: true, completion: nil)
}

func presentAlertController(title: String?, message: String?, preferredStyle: UIAlertControllerStyle,  handler: ((UIAlertAction) -> Swift.Void)? = nil, completion: (() -> Swift.Void)? = nil) {

    let alertController = UIAlertController(title: title, message: nil, preferredStyle: .actionSheet)
    alertController.addAction(UIAlertAction(title: "OK", style: .default) { (alertAction) -> Void in
        handler?(alertAction)
    })

    self.present(alertController, animated: true, completion: {
        self.addDismissControl(alertController.view)
        completion?()
    })
}

func someWhereInYourViewController() {
    // ...
    presentAlertController(title: "SomeTitle", message: "SomeMessage", preferredStyle: .actionSheet, handler: { (alertAction) -> Void in
        //do some action
    }, completion: {
        //do something after presentation
    })
    // ...
}

也许我错过了什么(我对iOS很陌生)。但根据我的经验,将UIAlertControllerStyleActionSheet与UIAlertActionStyleCancel一起使用似乎不会添加取消操作。将样式更改为UIAlertControllerStyleAlert始终显示取消操作。请注意,即使将其作为操作添加,UIAlertController在使用popover时也会删除取消按钮。用户通过触摸popover的外部来取消popover,因此它不是必需的。注意:如果使用UIAlertActionStyleCancel样式的操作,则只有在使用UIAlertControllerStyleActionSheet AlertController样式时,才会解除AlertController。它不适用于UIAlertControllerStyleAlert。@JDG是的,但无论如何不能通过外部点击取消警报样式,所以这无关紧要。当我为按钮操作提供它时,这是可以的,我现在将它用于uitextfield,所以我应该如何调用此控制器,我还尝试调用“-(BOOL)方法textField应该开始编辑:(UITextField*)textField{}“此时警报视图会反复打开,请帮助我…”。。我需要在textfield上触发它,谢谢。