let appDelegate = UIKit.UIApplication.shared.delegate!
if let tabBarController = appDelegate.window??.rootViewController as? UITabBarController {
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let signInVC = storyboard.instantiateViewController(withIdentifier: "SignInVC") as! SignInVC
guard !signInVC.isBeingPresented else {
log.warning("Attempt to present sign in sheet when it is already showing")
return
}
signInVC.modalPresentationStyle = UIModalPresentationStyle.formSheet
tabBarController.present(signInVC, animated: true, completion: nil)
}
尽管出现了
signInVC
,但可以多次调用此代码。我已经添加了此支票: guard !signInVC.isBeingPresented else {
log.warning("Attempt to present sign in sheet when it is already showing")
return
}
但这似乎并不能阻止这个错误:
Warning: Attempt to present <App.SignInVC: 0x101f2f280> on <UITabBarController: 0x101e05880> which is already presenting <App.SignInVC: 0x101f4e4c0>
最佳答案
您的guard
不是有效支票。isBeingPresented
正在一个全新的视图控制器实例上调用,该实例尚未呈现。因此isBeingPresented
将始终是false
。此外,该属性只能在视图控制器的view[Will|Did]Appear
方法中使用。
您要检查的是tabBarController
是否已经提供了另一个视图控制器。
最后,只创建和设置登录视图控制器(如果应该显示)。
let appDelegate = UIKit.UIApplication.shared.delegate!
if let tabBarController = appDelegate.window?.rootViewController as? UITabBarController {
if tabBarController.presentedViewController == nil {
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let signInVC = storyboard.instantiateViewController(withIdentifier: "SignInVC") as! SignInVC
signInVC.modalPresentationStyle = UIModalPresentationStyle.formSheet
tabBarController.present(signInVC, animated: true, completion: nil)
}
}
关于ios - 检查后是否仍显示“在演示时尝试演示”?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44222543/