Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertAction dismisses the UIAlertController in its callback [duplicate]

I am trying to use new UIAlertController introduced in iOS 8. Everything works great except the fact that UIAlertAction always end up dismissing the alert controller in its callback. Following is my code:

let alert = UIAlertController(title: "New Group", message: "Enter Group name", preferredStyle: UIAlertControllerStyle.Alert);
alert.addTextFieldWithConfigurationHandler({ [weak self] (nameField: UITextField!) in
    nameField.becomeFirstResponder();
    nameField.delegate = self;
    return;
})
alert.addAction(UIAlertAction(title: "Done", style: .Default, handler: { action in
    println("Done Entering");
}));
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil));
self.presentViewController(alert, animated: true, completion: nil);

Now, when I click "Done" button, the controls enters the callback method and then alert is dismissed even though I don't have any statement to dismiss the alert. Is this behavior by default? If yes, how can I make sure that in some cases the alert stays on the screen (depending upon my conditions)? Am I missing something here?

I would really appreciate any help regarding this.

like image 766
Vik Singh Avatar asked Dec 26 '22 04:12

Vik Singh


1 Answers

Yes, alert buttons always dismiss the alert and there is no way to configure them otherwise. If you want that behavior, you'll have to write your own alert. I've written SDCAlertView, which looks very much like a normal alert, but has some added functionality, including prevention of dismissing an alert when a button is tapped.

However, it doesn't use the UIAlertController API yet and it looks a little bit different (most users won't notice) on iOS 8 than the UIAlertController alert.

EDIT: It now has support for the UIAlertController-like API.

like image 98
Scott Berrevoets Avatar answered Apr 27 '23 07:04

Scott Berrevoets