我想解决一个问题。
我有一个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)
}
答案 0 :(得分:0)
您class A {
private String fieldOne;
private String fieldTwo;
private Integer fieldThree;
private Integer fieldFour;
}
为tabBarController
,并且您将其输入导航控制器,这是代码中的问题。您需要将rootViewController
转换为rootViewController
,然后在MainTabViewController
上获取所选标签,如果您有导航控制器,则将MainTabViewController
控制器推送到该导航控制器,否则您需要提交ProductListingVC
。