在UIAlertController之外轻触如何解除UIAlertController?

UIAlertController之外轻触如何解除UIAlertController

我可以添加UIAlertActionUIAlertActionStyleCancel来closuresUIAlertController

但是我想添加一个函数,当用户点击UIAlertControllerUIAlertController会解散。 怎么做? 谢谢。

使用样式UIAlertActionStyleCancel添加单独的取消操作。 所以,当用户在户外,你会得到callback。

  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,你可以使用下面的代码片断:

 func showAlertBtnClicked(sender: UIButton) { let alert = UIAlertController(title: "This is title", message: "This is message", preferredStyle: .Alert) self.presentViewController(alert, animated: true, completion:{ alert.view.superview?.userInteractionEnabled = true alert.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.alertControllerBackgroundTapped))) }) } func alertControllerBackgroundTapped() { self.dismissViewControllerAnimated(true, completion: nil) } 

随着迅捷3:

 func showAlertBtnClicked(sender: UIButton) { let alert = UIAlertController(title: "This is title", message: "This is message", preferredStyle: .alert) self.present(alert, animated: true) { alert.view.superview?.isUserInteractionEnabled = true alert.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.alertControllerBackgroundTapped))) } } func alertControllerBackgroundTapped() { self.dismiss(animated: true, completion: nil) } 

如果你正在使用Swift

使用addAction(_:)style:UIAlertActionStyle.Cancel添加一个动作。

当你点击button或在框架外面时,将会调用处理程序。

 var alertVC = UIAlertController(...) // initialize your Alert View Controller alertVC.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { (alertAction: UIAlertAction!) in alertVC.dismissViewControllerAnimated(true, completion: nil) })) 

Objective-C

 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:...]; [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { [alertVC dismissViewControllerAnimated:YES completion:nil]; }]]; 
  UIView *alertView = self.alertController.view; UIView *superPuperView = self.alertController.view.superview; CGPoint tapCoord = [tap locationInView:superPuperView]; if (!CGRectContainsPoint(alertView.frame, tapCoord)) { //dismiss alert view } 

最简单的方法是:

 - (void)viewDidLoad { [super viewDidLoad]; [self button]; } - (void) button { UIButton * AlertButton = [UIButton buttonWithType:UIButtonTypeSystem]; [AlertButton setTitle:@"Button" forState:UIControlStateNormal]; AlertButton.frame = CGRectMake((self.view.frame.size.width/2) - 50 , (self.view.frame.size.height/2) - 25, 100, 50); [AlertButton addTarget:self action:@selector(Alert) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:AlertButton]; } - (void)Alert { UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Alert Title" message:@"Alert Message" preferredStyle:UIAlertControllerStyleAlert]; [self presentViewController: alert animated: YES completion:^{ alert.view.superview.userInteractionEnabled = YES; [alert.view.superview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(DismissAlertByTab)]]; }]; } - (void)DismissAlertByTab { [self dismissViewControllerAnimated: YES completion: nil]; } 

Obj-C最简单的方法是:

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

接着:

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

如果您查看警报的超级视图的debugging,你会看到它不像添加一个轻拍手势识别器的UITransitionView的_UIAlertControllerViewView.You可以做到这一点

 [presenter presentViewController:alertController animated:YES completion:^{ NSArray <UIView *>* superviewSubviews = alertController.view.superview.subviews; for (UIView *subview in superviewSubviews) { if (CGRectEqualToRect(subview.bounds, weakSelf.view.bounds)) { [subview addSingleTapGestureWithTarget:weakSelf action:@selector(dismissModalTestViewController)]; } } }]; 
 - (void)addBackgroundDismissTapForAlert:(UIAlertController *)alert { if (!alert.view.superview) { return; } alert.view.superview.userInteractionEnabled = YES; [alert.view.superview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(alertControllerBackgroundTapped)]]; for (UIView *subV in alert.view.superview.subviews) { if (subV.width && subV.height) { subV.userInteractionEnabled = YES; [subV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(alertControllerBackgroundTapped)]]; } } } - (void)alertControllerBackgroundTapped { [self dismissViewControllerAnimated: YES completion: nil]; }