UIAlertView show() behavior for UIAlertController

做~自己de王妃 提交于 2019-11-29 05:05:25
Dylan Bettermann

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:

  1. Make your window the key and visible window (window.makeKeyAndVisible())
  2. Just use a plain UIViewController instance as the rootViewController of the new window. (window.rootViewController = UIViewController())
  3. Present your UIAlertController on your window's rootViewController

A couple things to note:

  • Your UIWindow must be strongly referenced. If it's not strongly referenced it will never appear (because it is released). I recommend using a property, but I've also had success with an associated object.
  • To ensure that the window appears above everything else (including system UIAlertControllers), I set the windowLevel. (window.windowLevel = UIWindowLevelAlert + 1)

Lastly, I have a completed implementation if you just want to look at that.

https://github.com/dbettermann/DBAlertController

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)
    }
}

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];

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!