dismiss UIAlertController presented by a modal view controller

孤街醉人 提交于 2020-01-02 03:00:07

问题


I seem to be running into a problem similar to one in an unresolved posted question: UIAlertController dismissing his presentingViewController

I am presenting a modal view controller on top of a normal UIViewController. Then I'm popping up an alert on that modal view controller. When I push "ok" to dismiss the alert (generated with the code below), the modal view controller is also dismissed.

UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK"
                                             style:UIAlertActionStyleDefault 
                                           handler:^(UIAlertAction *action{ 
                                                    [self dismissViewControllerAnimated: YES completion: nil];}];

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Sign up problem."
                                                               message:@"Some fields are empty. Please check your inputs and try again."
                                                        preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];

How can I dismiss just the alert?

I realize I can avoid this problem by using a navigation controller type setup instead and hiding the navigation bar, so I Get the same feel as the modal view controller, but this seems silly. Thanks.


回答1:


Don't call self dismissViewController in the button handler. That specifically states that you want the view controller dismissed.

You don't need to dismiss the alert. It will automatically dismiss itself. The only thing you should do in the button handler is perform whatever action you need. Do nothing if you don't need to do anything.

If your alert is simply a message and you don't need to perform any action, just do this:

UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];



回答2:


You don't need to dismiss or remove the UIAlertController manually in any way in a button handler - it does that itself.

Just remove the call to dismissViewControllerAnimated:completion:.



来源:https://stackoverflow.com/questions/27044474/dismiss-uialertcontroller-presented-by-a-modal-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!