Reputation: 763
In previous versions of iOS I was able to call show
on a UIAlertView in the App Delegate. More specifically, show
was called in:
func applicationDidBecomeActive(application: UIApplication)
Since UIAlertView
s disregarded the view hierarchy in most cases, this solution worked no matter where the user was in the app.
With the introduction of UIAlertController
this problem becomes a little trickier. UIAlertController
is now a subclass of UIViewController
and needs to be presented just like any other UIViewController. While presenting the UIAlertController from the keyWindow's rootViewController works, it's not the ideal solution.
Does anyone have any ideas on replicating [UIAlertView show]
functionality for a UIAlertController
? Any way to show the UIAlertController
on app active without traversing the view hierarchy?
Upvotes: 4
Views: 3278
Reputation: 11
Try This
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"title"
message:@" Your message hear"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okBtnAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
//Do what action u want.
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:okAction];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[alert dismissViewControllerAnimated:YES completion:nil];
//do something when click button
}];
[alert addAction:Action];
UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
[vc presentViewController:alert animated:YES completion:nil];
Upvotes: 1
Reputation: 2181
Try using JSAlertView which handles both UIAlertView and UIAlertController APIs. It provides short and easy methods for displaying alerts and handles multiple alerts fired at same time, very well.
Upvotes: -1
Reputation: 763
I figured out a solution that I believe to be more elegant than the answer I posted previously. I'll copy and paste the answer I posted to a similar question. 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.
https://github.com/dbettermann/DBAlertController
Upvotes: 4
Reputation: 763
Here's what I ended up with:
public class func visibleViewController() -> UIViewController? {
return self.visibleViewController(UIApplication.sharedApplication().keyWindow?.rootViewController?)
}
private class func visibleViewController(viewController: UIViewController?) -> UIViewController? {
if viewController?.presentedViewController == nil {
println("Visible view controller: \(viewController)")
return viewController
} else if let navigationController = viewController as? UINavigationController {
return self.visibleViewController(navigationController.topViewController)
} else if let tabBarController = viewController as? UITabBarController {
return self.visibleViewController(tabBarController.selectedViewController)
} else {
return self.visibleViewController(viewController?.presentedViewController)
}
}
Upvotes: 0