当用户点击通知托盘时,我需要导航到特定的 View Controller 。因此我需要从 AppDelegate 类重定向它们:
这是 AppDelegate 中的代码:
if let myVc = UIStoryboard(name : "Main",bundle : nil).instantiateViewController(withIdentifier: "MyViewController")
as? MyViewController {
myVc.postId = Int(postId)
if let navigationController = self.window?.rootViewController /* as? UINavigationController */{
navigationController.show(myVc, sender: Any?.self)
}
}
通过使用这段代码,当我点击通知托盘时,我可以重定向到我的 MyViewController,但问题是导航栏不见了,所以应用程序就卡在那里了。
我也尝试过使用它,但是通过下面的代码,它无法重定向到 myPostVc:
if let navigationController = self.window?.rootViewController as? UINavigationController {
navigationController.pushViewController(myVc, animated: true)
}
所以我的问题是:
如何从 AppDelegate 导航到特定的 View Controller ,导航栏出现在目标 View Controller 上?
最佳答案
将 tabBar 嵌入到导航 Controller 中(在代码中,当您选择一个 tabbar 并希望将其嵌入到 Storyboard 中的导航 Controller 中时,嵌入是灰色的)并使用它来推送 VC
if let navigationController = self.window?.rootViewController as? UINavigationController {
navigationController.pushViewController(myVc, animated: true)
}
或者你可以这样做
if let cu = self.window?.rootViewController as? UITabBarController
{
let nav: UINavigationController = UINavigationController()
self.window?.rootViewController = nav
let str = UIStoryboard.init(name: "Main", bundle: nil)
let rr = str.instantiateViewController(withIdentifier: "idOFPushedVC")
nav.setViewControllers([cu,rr], animated: true)
}
关于ios - 从 AppDelegate 导航时导航栏消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49223100/