gpt4 book ai didi

ios - 在没有 Storyboard的情况下点击通知时访问特定的 ViewController(以编程方式)

转载 作者:行者123 更新时间:2023-11-28 05:38:44 26 4
gpt4 key购买 nike

我有一个包含 3 个选项卡的应用程序:- 家- 收件箱- 简介

当用户收到通知时,我想打开相应的聊天,当用户按下后退按钮时,我希望能够打开收件箱标签。

我的以下代码是这样的:

     func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {

let notificationContent = response.notification.request.content

switch notificationContent.categoryIdentifier {

case "ChatViewController":

let tabBarVC = self.window?.rootViewController as! MainTabViewController

tabBarVC.selectedIndex = 1 // This is the Inbox tab

let chatVC = ChatViewController(collectionViewLayout: UICollectionViewFlowLayout())

let inboxVC = InboxViewController()

inboxVC.navigationController?.pushViewController(chatVC, animated: true)

default:
return
}


completionHandler()
}

但是在这行 tabBarVC.selectedIndex = 1 之后,代码没有被读取,它打开了 InboxViewController,这是为什么,我如何打开聊天?

最佳答案

使用

对选定的 Controller 进行检查太简单了
let selectedController = tabBarVC.selectedViewController? 

功能齐全

          func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {

let notificationContent = response.notification.request.content

switch notificationContent.categoryIdentifier {

case "ChatViewController":

let tabBarVC = self.window?.rootViewController as! MainTabViewController

tabBarVC.selectedIndex = 1 // This is the Inbox tab

let chatVC = ChatViewController(collectionViewLayout: UICollectionViewFlowLayout())

if let selectedController = tabBarVC?.selectedViewController as? InboxViewController
{
selectedController?.navigationController?.pushViewController(chatVC, animated: true)
}

default:
return
}


completionHandler()
}

关于ios - 在没有 Storyboard的情况下点击通知时访问特定的 ViewController(以编程方式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57686369/

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