检查后仍然出现“尝试在已经呈现时呈现”?
Posted
技术标签:
【中文标题】检查后仍然出现“尝试在已经呈现时呈现”?【英文标题】:"Attempt to present while already presenting" still appearing after checks? 【发布时间】:2017-05-27 23:44:08 【问题描述】: 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>
【问题讨论】:
【参考方案1】:您的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)
【讨论】:
这是否解决了您的问题或者您仍有问题?以上是关于检查后仍然出现“尝试在已经呈现时呈现”?的主要内容,如果未能解决你的问题,请参考以下文章
未定义符号:安装 firebase 库后出现 __isPlatformVersionAtLeast 错误