简体   繁体   English

使用 UITabBarController 而不是 UINavigationController 时返回上一个 ViewController

[英]Return to previous ViewController when using UITabBarController but not UINavigationController

Im using a UITabBarController that I've created programatically to present UIViewControllers that I've created in Storyboard.我使用我以编程方式创建的 UITabBarController 来呈现我在 Storyboard 中创建的 UIViewController。 I present these with我提出这些

let storyboard = UIStoryboard(name: "Main", bundle: nil)

// create tab one
let tabOne = storyboard.instantiateViewController(withIdentifier: "feedVC")
let tabOneBarItem = UITabBarItem(title: "feed", image: UIImage(named: "feed_icon"), selectedImage: UIImage(named: "feed_icon_selected"))
        tabOne.tabBarItem = tabOneBarItem

self.viewControllers = [tabOne]

in one of the tabs where I don't show the TabBar, I have a cancel button in the top left.在我没有显示 TabBar 的选项卡之一中,我在左上角有一个取消按钮。 I prefer this to send the user back to the previous ViewController.我更喜欢这样将用户发送回之前的 ViewController。 But settle with a specific one.但是解决一个特定的问题。 The code below works, but the TabBarController do not show... what is the most efficient way to implement a cancel/return button.下面的代码有效,但 TabBarController 没有显示......实现取消/返回按钮的最有效方法是什么。

Should I use UINavigationController - or are there better alternatives?我应该使用 UINavigationController - 还是有更好的选择?

@IBAction func cancelBtnWasPressed(_ sender: Any) {
    let returnVC = storyboard?.instantiateViewController(withIdentifier: "feedVC")


    present(returnVC!, animated: true, completion: nil)


}

I found the solution that worked for me.我找到了对我有用的解决方案。

Swift 4:斯威夫特 4:

self.tabBarController?.selectedIndex = 0

This moves the view back to the first of the Tabs i the TabBarController这将视图移回 TabBarController 中的第一个选项卡

You have to dismiss the current one :您必须dismiss当前的:

@IBAction func cancelBtnWasPressed(_ sender: Any) {
    self.dismiss(animated: true, completion: nil)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将UITabBarController与UINavigationController一起使用-Swift 3 - Using UITabBarController with UINavigationController - Swift 3 一起使用UITabBarController和UINavigationController - Using a UITabBarController and a UINavigationController together 如何将数据从ViewController传递到UITabBarController-> UINavigationController-> ViewController - How to passes data from ViewController to UITabBarController -> UINavigationController -> ViewController 在UITabBarController中使用UINavigationController时,UISearchBar会被切断 - UISearchBar gets cut off when using a UINavigationController inside a UITabBarController 具有UINavigationController的UITabBarController - UITabBarController with UINavigationController 使用UINavigationController iOS 6弹回到初始viewcontroller时,方向会发生变化 - Orientation changes when pop back to the initial viewcontroller using UINavigationController iOS 6 UITabbarController中的switch选项卡时,popToRoot UINavigationController - popToRoot UINavigationController when switch tab in UITabbarController 嵌入UINavigationController时不出现UITabbarController UITabbarItem - UITabbarController UITabbarItem does not appear when UINavigationController is embeded 我想使用委托创建UITabbarController,UINavigationController - I want create UITabbarController,UINavigationController using delegates 使用超过1个UINavigationController和仅一个UITabBarController - Using more than 1 UINavigationController and only one UITabBarController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM