gpt4 book ai didi

ios - 如何创建以 TabBarViewController 为根的导航堆栈?

转载 作者:行者123 更新时间:2023-11-29 01:14:24 27 4
gpt4 key购买 nike

我的 TabBarVC 是我的应用程序的核心。我有 3 个 View Controller 。每个 View Controller 都“嵌入”在导航 Controller 中。

TabBarVC -> UINavigationController -> HomeVC
-> UINavigationController -> TimelineVC
-> UINavigationController -> ConversationVC

我希望我的 TabBarVC 能够在需要时将 InstantMessageViewController 插入其堆栈。我知道 TabBarVC 没有“堆栈”或 NavigationController——而且只有它的子项有导航 Controller 。

TabBarVC 接收到 NSNotification 时,我想将 InstantMessageViewController 压入其堆栈。

也许 TabBarVC 中有这样的东西?

let mvc = self?.storyboard?.instantiateViewControllerWithIdentifier("MessagesViewController") as! InstantMessagesViewController
mvc.user = self?.notificationUser
self?.navigationController?.pushViewController(mvc, animated: true) //it doens't work because self.navigationController is nil.

当用户按下后退按钮时,它将返回到 TabBarVC。

我希望能够随时将多个 InstantMessagesViewController 压入堆栈并以编程方式重新排序。

我不知道什么是最好的方法。我创建一个 UIWIndow 吗?我是否以编程方式创建 UINavigationController?

最佳答案

您可以做的一件事是将 InstantMessagesViewController 推送到当前在 UITabBarController 上选择的任何 UINavigationController 上。

// Inside your UITabBarController
if let navigationController = selectedViewController {
let mvc = self?.storyboard?.instantiateViewControllerWithIdentifier("MessagesViewController") as! InstantMessagesViewController
mvc.user = self?.notificationUser
navigationController.pushViewController(mvc, animated: true)
}

但正确的做法是以模态方式呈现 InstantMessagesViewController (并将后续的 InstantMessagesViewController 推送到此 UINavigationController 堆栈上):

let mvc = self?.storyboard?.instantiateViewControllerWithIdentifier("MessagesViewController") as! InstantMessagesViewController
mvc.user = self?.notificationUser
let navController = UINavigationController(rootViewController: mvc)
presentViewController(navController, animated: true, completion: nil)

为了在您的根 InstantMessagesViewController 上关闭此 UINavigationController,请在 InstantMessagesViewController 中尝试以下代码:

override func viewDidLoad() {
super.viewDidLoad()

if let navigationController = navigationController {
if navigationController.viewControllers.count == 1 {
let barButton = UIBarButtonItem(title: "Back", style: .Plain, target: self, action: "onBackTapped:")
navigationItem.leftBarButtonItem = barButton
}
}
}

func onBackTapped(barButtonItem: UIBarButtonItem) {
dismissViewControllerAnimated(true, completion: nil)
}

关于ios - 如何创建以 TabBarViewController 为根的导航堆栈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35400622/

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