Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertController not appearing at all

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?

like image 297
Magna Boss Avatar asked Sep 27 '15 04:09

Magna Boss


People also ask

How to show alert in swift?

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.

How do you dismiss an alert with click on outside of the alert IOS?

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.

How do you dismiss an alert controller in Swift?

You can dismiss the alert by calling dismissViewControllerAnimated method on alertController object.


1 Answers

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 - succeeded
  • viewWillAppear - 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 ().
like image 137
Sheamus Avatar answered Oct 22 '22 05:10

Sheamus