Scenario: The user taps on a button on a view controller. The view controller is the topmost (obviously) in the navigation stack. The tap invokes a utility class method called on another class. A bad thing happens there and I want to display an alert right there before control returns to the view controller.
+ (void)myUtilityMethod {
// do stuff
// something bad happened, display an alert.
}
This was possible with UIAlertView
(but perhaps not quite proper).
In this case, how do you present a UIAlertController
, right there in myUtilityMethod
?
I posted a similar question a couple months ago and think I've finally solved the problem. Follow the link at the bottom of my post if you just want to see the code.
The solution is to use an additional UIWindow.
When you want to display your UIAlertController:
window.makeKeyAndVisible()
)window.rootViewController = UIViewController()
)A couple things to note:
window.windowLevel = UIWindowLevelAlert + 1
)Lastly, I have a completed implementation if you just want to look at that.