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

问题描述 投票:0回答:1

我正在使用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!
swift firebase
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.