UIAlertController的show()方法与UIAlertView的行为有何不同?

4

在之前的iOS版本中,我可以在应用代理(App Delegate)中调用show方法来显示UIAlertView。更具体地说,show方法被调用于:

func applicationDidBecomeActive(application: UIApplication)

由于在大多数情况下,UIAlertView 忽略了视图层次结构,因此这个解决方案可以在用户在应用程序中的任何位置使用。

随着 UIAlertController 的引入,这个问题变得有点棘手。现在,UIAlertControllerUIViewController 的子类,并且需要像任何其他 UIViewController 一样进行呈现。虽然从 keyWindow 的 rootViewController 呈现 UIAlertController 是可行的,但并不是理想的解决方案。

是否有人有任何想法来复制 [UIAlertView show] 功能以用于 UIAlertController?有什么方法可以在不遍历视图层次结构的情况下在应用程序处于活动状态时显示 UIAlertController


您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Dylan Bettermann
@DylanBettermann,为什么不从可见的视图控制器中调用UIAlertView呢? - brandonscript
1
如果您真的不关心用户体验或它适用于您的应用程序类型,可以从rootViewController呈现它。 - pronebird
@Andy 这个应用程序没有通过App Store分发,因此没有自动更新机制。我不明白这与不关心用户体验有什么关系。 - Dylan Bettermann
显示剩余6条评论
4个回答

4
我找到了一个解决方案,我认为它比我之前发布的答案更加优雅。 我将复制并粘贴我在similar question中发布的答案。如果您只想查看代码,请按照我的帖子底部的链接。

解决方案是使用另一个UIWindow。

当您想显示UIAlertController时:

  1. 使您的窗口成为关键窗口并可见(window.makeKeyAndVisible()
  2. 仅使用普通的UIViewController实例作为新窗口的rootViewController。(window.rootViewController = UIViewController()
  3. 在您的窗口的rootViewController上呈现UIAlertController

需要注意的几件事:

  • 您的UIWindow必须被强引用。如果没有被强引用,它将永远不会出现(因为它已被释放)。我建议使用属性,但我也成功地使用了关联对象
  • 为确保窗口出现在所有其他内容上方(包括系统UIAlertControllers),我设置了窗口级别。(window.windowLevel = UIWindowLevelAlert + 1

最后,如果您只想查看已完成的实现,请参考此处。

https://github.com/dbettermann/DBAlertController


0
试试这个。
    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];

0

这是我最终得出的结果:

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

-1
尝试使用JSAlertView,它可以处理UIAlertView和UIAlertController API。它提供了简短易懂的方法来显示警报,并且能够很好地处理同时触发的多个警报。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接