我面临的问题是,当我想以编程方式呈现 View 时,我的应用程序崩溃/显示空的黑色 View 。
vc 的打开发生在用户点击通知时。 我有一个标签栏、一个导航 Controller 和一个可以从标签栏中的第一个 vc 访问的 View 。
当我想推送 vc 时,我的应用程序崩溃了,但是当我删除要推送到 vc (ChatVC) 中的 viewDidLoad() 中的所有内容时,我的应用程序显示 vc,但它是空的和黑色的。 我的应用程序委托(delegate)推送代码。 这与 chatvc 无关,我尝试了我拥有的每个 View Controller ,但它都在同一点崩溃。
let chatVc = AllChatsViewController()
if let tabbarControler = window?.rootViewController as? MainTabBarController {
tabbarControler.selectedIndex = 0
//tabbarControler.presentedViewController?.dismiss(animated: true, completion: nil)
if let homeNav = tabbarControler.viewControllers?.first as? UINavigationController {
homeNav.pushViewController(chatVc, animated: true)
print(followerId)
}
}
}
更新
if let chatPartnerId = userInfo["chatPartnerId"] {
let storyboard = UIStoryboard(name:"Chat", bundle: Bundle.main)
guard let chatVC = storyboard.instantiateViewController(withIdentifier: "MessageViewController") as? MessageViewController else {return}
chatVC.userId = chatPartnerId as! String
if let tabbarControler = window?.rootViewController as? MainTabBarController {
tabbarControler.selectedIndex = 0
tabbarControler.presentedViewController?.dismiss(animated: true, completion: nil)
if let homeNav = tabbarControler.viewControllers?.first as? UINavigationController {
homeNav.pushViewController(chatVC, animated: true)
}
}
它现在按预期推送到 ViewController,但 viewDidLoad 或任何其他函数都不会被调用,任何想法?
最佳答案
你试过而不是
让 chatVc = AllChatsViewController()
从 Storyboard 中实例化它?在 Storyboard 中找到您的 viewController 并为它添加一个标识符(我将使用 chatVC)。然后
let storyboard = UIStoryboard(name:"Main", bundle Bundle.main)
让 chatVC = storyboard.instantiateViewController(withIdentifier: "chatVC")
原因是因为如果使用上面的方法,你所有的 subview 都会自动启动和布局,而不是你需要自己做的方法(你可能没有这样做,因此崩溃,事实上如果您删除 View 确实加载了调用您的 subview 的行,它就会通过。)
关于swift - AppDelegate 中的当前 viewController 崩溃/空白屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47781164/