Reject UIAlertControllerStyleAlert UIAlertController by clicking outside

As everyone knows, it is UIAlertView

now outdated and Apple wants us to use the new one UIAlertController

with style UIAlertControllerStyleAlert

. However, using UIAlertControllerStyleAlert

, you cannot invoke UIAlertAction

with style UIAlertActionStyleCancel

by touching the appearance of the alert.

Does anyone know a way to dismiss an alert view by clicking on it?

Greetings

+3


source to share


2 answers


You can add a separate UIAlertActionStyleCancel style cancel action for the alertViewController so that when the user logs out from the outside, you get a 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
 }]];

      

+2


source


Try this code:



- (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];
}

      

+1


source







All Articles