繁体   English   中英

使用 UITabBarController 而不是 UINavigationController 时返回上一个 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 没有显示......实现取消/返回按钮的最有效方法是什么。

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

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


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


}

我找到了对我有用的解决方案。

斯威夫特 4:

self.tabBarController?.selectedIndex = 0

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

您必须dismiss当前的:

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM