swift4 - 从 AppDelegate 调用 navigationController
问题描述
现在我正在实现一个信号通知,当用户单击通知时,我想打开特定的 Viewcontroller。
这是我在 AppDelegate 的代码
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let detailBrand = storyBoard.instantiateViewController(withIdentifier: "PagerOverviewControlerID") as! PagerOverviewControler
detailBrand.getValue = value
navigationController?.pushViewController(detailBrand, animated: true)
如果我将它放在任何 ViewController 类中但在 AppDelegate 中却没有,则此代码可以正常工作。
请帮忙!!!
解决方案
好吧,首先你必须明白Appdelgate
不是 a UIViewController
,
因此您不能使用pushViewController(detailBrand, animated: true)
,因为您并不完全UIVewController
能够这样做,但是您可以将其UINavigatetionController
设置为您的根,然后从中推送。
你的代码应该是这样的
let rootViewController = self.window!.rootViewController as! UINavigationController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let profileViewController = mainStoryboard.instantiateViewController(withIdentifier: "PagerOverviewControlerID") as! PagerOverviewControler
rootViewController.pushViewController(profileViewController, animated: true)
另请记住,您应该将其放在didFinishLaunchWithOption
方法中。
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool
推荐阅读
- javascript - 在 Firebase 的实时数据库中,如何将自己的参数传递给事件侦听器的回调?
- sqlite - 如何在 Flutter 中使用 Sqlite 将布尔值转换为 Int 值?
- javascript - 排序加密货币价格的问题(vue.js)
- css - 如何使伪元素跨换行符跟随文本?
- r - 根据其他下拉选择动态设置下拉选项
- python - int.to_bytes() 的行为不一致
- java - Java 仅调用以 parent 作为其函数参数的方法
- node.js - 如何将构建文件从一个容器复制到另一个容器或 docker 上的主机
- reactjs - 将 ReactJs 应用程序部署为静态应用程序以在 U 盘上使用
- rust - 在不同集合中存储对相同项目的引用:移动后借用的值