将UIAlert从一个类显示到当前查看的Controller Swift

时间:2017-12-16 23:53:21

标签: swift firebase

我正在使用swift从Firebase读取值,然后在用户导航应用程序的其余部分时继续观察它。当值更改时,它会显示警报,但无论您使用何种视图控制器,都会发生这种情况。本质上,id喜欢能够在任何视图控制器上显示此警报,尽管不在呈现它的人身上。

任何帮助?

 self.UserIsBlocked(userId: id){(blocked, value, success) in
        if success == true {
            if blocked == true {
                let time = String(describing: value)
                let blocked = UIAlertController(title: "Alert!", message: "Message to be displayed", preferredStyle: UIAlertControllerStyle.alert)
                self.present(blocked, animated: true, completion: nil)
                let ban = (value as! Int) * 60
                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.seconds(ban)){
                    blocked.dismiss(animated: true, completion: nil)
                }
            }
        }

编辑: 当我尝试UIApplication.shared.keyWindow?.rootViewController?.present(blocked, animated: true, completion: nil)时收到此错误:

Warning: Attempt to present <UIAlertController: 0x1028ad800> on <Greek_Life.LoginController: 0x103012400> whose view is not in the window hierarchy!

1 个答案:

答案 0 :(得分:0)

我意识到UIApplication.shared.keyWindow?.rootViewController?只是呈现初始视图控制器所以在下拉列表中我看到UIApplication.shared.keyWindow?.currentViewController()并且这个工作