iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – UIAlertController在外部点击时处理解除(IPad)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在iOS8之前,我们使用UIActionSheet来显示警报,现在我们需要使用UIAlertController.

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

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

解决方法

您可以使用样式添加动作: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];
}

大佬总结

以上是大佬教程为你收集整理的ios – UIAlertController在外部点击时处理解除(IPad)全部内容,希望文章能够帮你解决ios – UIAlertController在外部点击时处理解除(IPad)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: