I am trying to add an UIAlertController
to my app but it's not appearing at all. I have tried the following:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Message" message:@"Web Service is not available." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:ok];
[self presentViewController:alertController animated:YES completion:nil];
but this does not appear at all, what am I doing wrong?
To create an action button that the user can tap on, we will need to create a new instance of an UIAlertAction class and add it to our alert object. To add one more button to UIAlertController simply create a new UIAlertAction object and add it to the alert.
Step 1 − Open Xcode and create a single view application and name it UIAlertSample. So basically, when we tap on the button an alert will be displayed, when the user taps outside the alert the alert will be dismissing.
You can dismiss the alert by calling dismissViewControllerAnimated method on alertController object.
In a storyboard's initial view controller, this has to be in viewDidAppear:
. Otherwise if used in a XIB, it will also show an alert in viewWillAppear
and viewDidLoad
.
I ran your code in the Single View Application template in both iOS 8 and 9, putting your code into the given ViewController,
in the following view life cycle callbacks:
viewDidAppear
- succeededviewWillAppear
- no alert shown; resulted in this output in the console: Warning: Attempt to present on whose view is not in the window hierarchy!viewDidLoad
- no alert shown; resulted in this output in the console: Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior ().If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With