gpt4 book ai didi

ios - 从 AppDelegate 内部执行 pushViewController

转载 作者:行者123 更新时间:2023-11-29 11:44:46 25 4
gpt4 key购买 nike

在我的 iOS 应用程序中,我想在推送通知事件到达时打开一个新的 View Controller 。我尝试在 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 的导航。我怎样才能使它工作?

所以在这种情况下,我的 View Controller 层次结构不包含 UiNavigationController。层次结构如下。

enter image description here

最佳答案

Else block 会给你一个答案。

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")
}

关于ios - 从 AppDelegate 内部执行 pushViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44498881/

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