在我的iOS应用中,我想在推送通知事件到达时打开一个新的视图控制器。我试着在AppDelegate.swift里面跟踪以处理导航。
let friendStoryboard = UIStoryboard(name: "Profile", bundle: nil)
let friendsVC = friendStoryboard.instantiateViewController(withIdentifier: "FriendsViewIdentifier") as! FriendsViewController
let backItem = UIBarButtonItem()
backItem.title = ""
self.window?.rootViewController?.navigationItem.backBarButtonItem = backItem
self.window?.rootViewController?.navigationController?.pushViewController(friendsVC, animated: true)
但它仍无法处理View Controller的导航。我怎样才能做到这一点?
所以在这种情况下我的视图控制器层次结构不包含UiNavigationController。层次结构如下。
答案 0 :(得分:1)
其他区块会给你答案。
if let friendStoryboard: UIStoryboard = UIStoryboard(name: "Profile", bundle: nil) {
if let friendsVC = friendStoryboard.instantiateViewController(withIdentifier: "FriendsViewIdentifier") as? FriendsViewController {
let backItem = UIBarButtonItem()
backItem.title = ""
self.window?.rootViewController?.navigationItem.backBarButtonItem = backItem
if let navController = self.window?.rootViewController as? UINavigationController {
navController.pushViewController(friendsVC, animated: true)
} else if let tabController = self.window?.rootViewController as? UITabBarController {
if let tabViewControllers = tabController.viewControllers {
if let navController = tabViewControllers.first as? UINavigationController {
navController.pushViewController(friendsVC, animated: true)
} else {
print("tabViewControllers.first as? UINavigationController not found")
}
} else {
print("tabViewControllers count is zero")
}
} else {
print("UINavigationController & UITabBarController not found at self.window?.rootViewController")
}
} else {
print("FriendsViewController not found")
}
} else {
print("friendStoryboard not found")
}