7

I would like to present a UIAlertController with 2 buttons.

One button should close the alert and the second should perform an action but remain the alert on screen. Is there some configuration that can be done to action that it won't close the alert?

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"title"
                                                               message:@"message"
                                                        preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Do Something"
                                          style:UIAlertActionStyleDefault
                                        handler:^(UIAlertAction *action) {
    //Pressing this button, should not remove alert from screen
                                        }]];

[alert addAction:[UIAlertAction actionWithTitle:@"Close"
                                          style:UIAlertActionStyleDefault
                                        handler:^(UIAlertAction *action) {
    //Regular functionality- pressing removes alert from screen 
                                        }]];


[alert show];

This (Prevent UIAlertController to dismiss) was suggested as possible answer, but the question deals with text field. I need one of the action buttons to not close the alert when pressed.

Luda
  • 9,237
  • 11
  • 67
  • 121
  • 2
    Possible duplicate of [Prevent UIAlertController to dismiss](https://stackoverflow.com/questions/28919670/prevent-uialertcontroller-to-dismiss) – Kevinosaurio May 03 '18 at 15:09
  • 2
    Note that the action handler isn't called until the alert is already dismissed. – rmaddy May 03 '18 at 16:01
  • 1
    @Luda its better to use custom view. not alertcontroller. – iNiravKotecha May 03 '18 at 18:12
  • My suggestion is create a custom view similar native view of UIAlertController, the completionHandler of UIAlertAction is called after alert dismissed. Hope helped. – MMSousa May 06 '18 at 23:50
  • why don't you show the alert again after it dismissed ? :D – Arash Etemad May 07 '18 at 18:36
  • in Do Something action you want to iterates with textfield.Right? – RB's May 08 '18 at 04:20
  • @arash Title - New privacy policy. Button 1: Read (not dismiss popup). Button 2: Approve. – Luda May 08 '18 at 08:16
  • @RB1509 No. I want this: Title - New privacy policy. Button 1: Read (not dismiss popup). Button 2: Approve. – Luda May 08 '18 at 08:17
  • Besides the linked dupe with the gesture recognizer override, it used to be possible to shove custom views into alert views. I don't know if that is still possible or desired with the way it's now a Controller object and not necessarily just a view currently. – Stephen J May 09 '18 at 21:14

5 Answers5

6

You can't do this.

Only one solution is to create a custom view controller that will look like native UIAlertController.

Ivan Smetanin
  • 1,884
  • 2
  • 17
  • 28
3

You can't do it with default UIAlertViewController, if you want to do this you need to create custom view controller whose look like UIAlertController.
You can use this custom view controller.

https://github.com/nealyoung/NYAlertViewController

MobileMatrix
  • 152
  • 11
1

Speaking from a user-perspective, a button press that doesn't follow through with an action would make me wonder if something was broken. If you're trying to use this as an opportunity to get further information, or some detail about the button press intention, I think a solution that adheres to user-expectation (though maybe a little annoying?) is to simply present a second dialog box after closing out the first. Basically, a "one-dialog-box-per-question" way of handling it.

Otherwise, I'll agree with the other suggestions and say that you need a custom view here, not a stock UIKit solution.

christopherdrum
  • 1,453
  • 9
  • 25
1

Try this code.

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"title"
                                                               message:@"message"
                                                        preferredStyle:UIAlertControllerStyleAlert];

[alert addAction:[UIAlertAction actionWithTitle:@"Do Something"
                                          style:UIAlertActionStyleDefault
                                        handler:^(UIAlertAction *action) {

                                            NSLog(@"Do Something Tapped");
                                        }]];

[alert addAction:[UIAlertAction actionWithTitle:@"Close"
                                          style:UIAlertActionStyleDefault
                                        handler:^(UIAlertAction *action) {
                                            NSLog(@"Close Tapped");
                                        }]];


[self presentViewController:alert animated:YES completion:nil];
Rohit Makwana
  • 2,835
  • 12
  • 23
1

How about this code:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"title"
                                                               message:@"message"
                                                        preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *doSomethingAction = [UIAlertAction actionWithTitle:@"Do Something"
                                                     style:UIAlertActionStyleDefault
                                                   handler:nil];
doSomethingAction.enabled = NO;
[alert addAction:doSomethingAction];

[alert addAction:[UIAlertAction actionWithTitle:@"Close"
                                          style:UIAlertActionStyleDefault
                                        handler:^(UIAlertAction *action) {
                                            //Regular functionality- pressing removes alert from screen
                                        }]];
[self presentViewController:alert animated:true completion:nil];

set NO to enabled property of UIAlertAction. It works well.

Kazunori Takaishi
  • 1,831
  • 1
  • 12
  • 21