gpt4 book ai didi

iOS Swift 从推送通知以编程方式导航到某些 ViewController

转载 作者:可可西里 更新时间:2023-11-01 00:41:33 27 4
gpt4 key购买 nike

我使用推送通知来通知用户应用中发生的不同类型的事件。某种类型的推送通知应该打开一个特殊的 View Controller (例如,关于新聊天消息的通知确实会在单击时导航到聊天)

为此,我在我的应用委托(delegate)中尝试了以下代码:

func applicationDidBecomeActive(_ application: UIApplication) {
if var topController = UIApplication.shared.keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
topController.tabBarController?.selectedIndex = 3
}
}

它不会移动到任何地方。我在这里缺少什么?

最佳答案

我编写了一个简单的类,只需传递类类型,即可从一行代码中的任何位置导航到 View 层次结构中的任何 View Controller ,因此您将编写的代码也将与 View 层次结构本身分离,因为实例:

Navigator.find(MyViewController.self)?.doSomethingSync()
Navigator.navigate(to: MyViewController.self)?.doSomethingSync()

..或者您也可以在主线程上异步执行方法:

Navigator.navigate(to: MyViewController.self) { (MyViewControllerContainer, MyViewControllerInstance) in
MyViewControllerInstance?.doSomethingAsync()
}

这是 GitHub 项目链接:https://github.com/oblq/Navigator

关于iOS Swift 从推送通知以编程方式导航到某些 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43058261/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com