gpt4 book ai didi

ios - 从 AppDelegate 向 ViewController 推送远程通知

转载 作者:行者123 更新时间:2023-11-30 11:57:00 25 4
gpt4 key购买 nike

我有一个问题正在尝试解决。我有一个标签栏作为我的 rootViewController 定义如下窗口?.rootViewController = MainTabViewController()我需要使用下面的代码片段从远程推送通知推送到 viewController,但我的应用程序崩溃 cannot Cast MainTabViewController to UINavigationViewController 我知道我无法执行该转换,因为 MainTabViewController 未嵌入 NavigationViewController 中。解决这个问题的最佳方法是什么?

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

let userInfo = response.notification.request.content.userInfo

if let type = userInfo["type"] as? String, let data = userInfo["data"] as? String, let title = userInfo["title"] as? String {
let params = ["type": type, "data": data, "title": title]
Analytics.AddEvent(title: "Notification Open", params: params)

let backItem = UIBarButtonItem()
backItem.title = ""
let navVC = window?.rootViewController as! UINavigationController
NSLog("Controller Type: \(navVC.description)")
navVC.navigationController?.navigationBar.tintColor = .black
navVC.navigationItem.backBarButtonItem = backItem
if type == "category_product"{
//Show product list
var params = [String: String]()
params["CategoryName"] = title
params["CategoryId"] = data
Analytics.AddEvent(title: "Category Opened", params: params)

let vc = ProductListingVC()
vc.categoryName = title
vc.category_id = data
vc.hidesBottomBarWhenPushed = false
navVC.pushViewController(vc, animated: true)
}

最佳答案

您将 tabBarController 作为 rootViewController 并且将其类型转换为导航 Controller ,这是代码中的问题。您需要将 rootViewController 转换为 MainTabViewController,然后在 MainTabViewController 上获取所选选项卡(如果您有导航 Controller ),然后推送 ProductListingVC Controller 到该导航 Controller ,否则您需要呈现 ProductListingVC

关于ios - 从 AppDelegate 向 ViewController 推送远程通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47694123/

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