当使用 UITabBarController 而不是 UINavigationController 时 Return 到之前的 ViewController

Return to previous ViewController when using UITabBarController but not UINavigationController

我正在使用以编程方式创建的 UITabBarController 来呈现我在 Storyboard 中创建的 UIViewController。我用

呈现这些
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]

在其中一个没有显示 TabBar 的选项卡中,我在左上角有一个取消按钮。我更喜欢这样将用户送回之前的 ViewController。但是要解决一个特定的问题。下面的代码有效,但 TabBarController 不显示...实现 cancel/return 按钮的最有效方法是什么。

我应该使用 UINavigationController - 还是有更好的选择?

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


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


}

你必须dismiss当前的:

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

我找到了适合我的解决方案。

Swift 4:

self.tabBarController?.selectedIndex = 0

这会将视图移回 TabBarController 中的第一个选项卡