Dont dismiss UIAlertController

1.1k Views Asked by At

I currently have a UIAlertController that I need to make non-dismissible. The Alert should not dismiss when pressing the action button.

How can I do this?

 UIAlertController *alert;

    int bestScore = [[[NSUserDefaults standardUserDefaults] objectForKey:@"bestScore"] intValue];
    if (!bestScore || bestScore < _score){
        [[NSUserDefaults standardUserDefaults] setObject:@(_score) forKey:@"bestScore"];
        alert = [UIAlertController alertControllerWithTitle:@"GAME OVER "
                                                    message:[NSString stringWithFormat:@"NEW RECORD! \n SCORE : %d  \n\n\n\n\n\n", _score] preferredStyle:UIAlertControllerStyleActionSheet];
    }
    else   alert = [UIAlertController alertControllerWithTitle:@"GAME OVER"
                                                       message:[NSString stringWithFormat:@"SCORE : %d \n Best score : %d  \n\n\n\n\n\n ", _score, bestScore] preferredStyle:UIAlertControllerStyleAlert];

    [alert addAction:[UIAlertAction actionWithTitle:@"Try again" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self newGame];
        [self addNewView];

    }]];


      [alert addAction:[UIAlertAction actionWithTitle:@"Answer" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                [alert viewWillDisappear:NO];

            }]];
 [self presentViewController:alert animated:YES completion:nil];
3

There are 3 best solutions below

0
On

I would recommend creating a UIViewController that contains a UIView. Within this UIView you will be able to display your required information and add the custom button actions that you desire.

In order to make the UIView appear like a modal view controller add a UIVisualEffectView with a UIBlurEffectStyle.

This is as simple as creating a conventional view controller using your storyboard/xib adding the required UIView in the interface builder then linking up the associated view controller class. After completing the initial setup and user interface add the following code to viewDidLoad. Furthermore you can perform the required animations etc within viewWillAppear & viewWillDisappear.

UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]
UIVisualEffectsView *blurView = [[UIVisualEffectView alloc] initWithEffect:effect];
[self.view insertSubview:blurView atIndex:0];
0
On

You can do like this

If you dont want dismiss alertController then You can disabled button actions of it like

alert.actions[1].enabled = NO

This will make your alertController Non-dismissable.

0
On

simply present the alertContoller again at the start of the action for alertButton you want it to not dismiss,

[self presentViewController:alertController animated:YES completion:nil];