在UIAlertController外部点击时如何关闭UIAlertController? [英] How to dismiss UIAlertController when tap outside the UIAlertController?

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

问题描述

UIAlertController之外点击时如何关闭UIAlertController?

How to dismiss UIAlertController when tap outside the UIAlertController?

我可以添加样式UIAlertActionStyleCancelUIAlertAction以关闭UIAlertController.

I can add a UIAlertAction of style UIAlertActionStyleCancel to dismiss the UIAlertController.

但是我想添加一个功能,当用户在UIAlertController之外点击时,UIAlertController将关闭.怎么做?谢谢.

But I want to add the function that when user tap outside the UIAlertController the UIAlertController will dismiss. How to do that? Thank you.

推荐答案

添加样式为UIAlertActionStyleCancel的单独的取消动作.这样,当用户在外面轻按时,您将获得回调.

Add a separate cancel action with style UIAlertActionStyleCancel. So that when user taps outside, you would get the callback.

Obj-c

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert Title" message:@"A Message" preferredStyle:UIAlertControllerStyleActionSheet];
[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
 // Called when user taps outside
}]];

Swift 5.0

let alertController = UIAlertController(title: "Alert Title", message: "A Message", preferredStyle: .actionSheet)             
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { 
    action in
         // Called when user taps outside
}))

这篇关于在UIAlertController外部点击时如何关闭UIAlertController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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