我试图解决一个问题 . 我有一个tabbar作为我的 rootViewController
定义像这样 window?.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)
}
1 回答
您有
tabBarController
asrootViewController
并且您将其类型转换为导航控制器,这是您的代码中的问题 . 您需要将rootViewController
转换为MainTabViewController
,然后在MainTabViewController
上获取所选标签,如果您有导航控制器,则将ProductListingVC
控制器推送到该导航控制器,否则您需要提供ProductListingVC
.