【问题标题】:Return to previous ViewController when using UITabBarController but not UINavigationController使用 UITabBarController 但不使用 UINavigationController 时返回上一个 ViewController
【发布时间】:2017-10-29 23:08:31
【问题描述】:

我正在使用我以编程方式创建的 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)


}

【问题讨论】:

    标签: ios xcode navigation nav swift4


    【解决方案1】:

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

    斯威夫特 4:

    self.tabBarController?.selectedIndex = 0
    

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

    【讨论】:

      【解决方案2】:

      你必须dismiss当前的:

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

      【讨论】:

      • 我试过这个。它没有影响。它与我需要关闭的 Viewcontroller 有什么关系吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-03
      • 2017-05-24
      • 2020-04-28
      • 2012-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多