【问题标题】:Segue from ViewController to first tab of TabBarController从 ViewController 转到 TabBarController 的第一个选项卡
【发布时间】:2016-11-12 22:22:16
【问题描述】:

我创建了一个选项卡式应用程序。在第一个选项卡 (ViewController+TableView) 中,它显示了从 DB 获取的结果。我有一个按钮可以打开另一个 ViewController,用户可以在其中过滤结果(例如“城市”)。

现在,我怎样才能使用 segue 返回到第一个标签页?因为我需要传递参数才能对 DB 进行查询。如果我在此页面和第一个 ViewController 之间创建一个 segue,它不会显示选项卡式菜单。

谢谢你们。

【问题讨论】:

  • 你需要使用 unwind segue 才能返回

标签: ios swift uitabbarcontroller swift3


【解决方案1】:

我假设您想要实现的是将数据从一个选项卡传递到另一个选项卡。

“现在,我怎样才能返回到第一个标签页有一个转场? 因为我需要传递参数才能对 DB 进行查询。"

实际上,您不必执行 segue 来传递数据,而是可以执行以下操作:

// somewhere in your code when you need to pass data to another tab viewController:
if let tabBarController = tabBarController {
    let secondTabViewController = tabBarController.viewControllers![1] as! SecondViewController
    // let's assume that you want to pass a string
    secondTabViewController.str = "my str!!"
}

第二视图控制器:

class SecondViewController: UIViewController {
    // here is the string that had been passed from the first tab viewController:
    var str:String?

    override func viewDidLoad() {
        super.viewDidLoad()

        // note that the output is: "Optional("my str!!")"
        print(str)

        // do optional binding:
        if let unwrappedStr = str {
            // output is: "my str!!"
            print(unwrappedStr)
        }
    }
}

希望对你有所帮助。

【讨论】:

  • 这不是我的意思.. 我使用了 Unwind segue 并且它有效! :) 谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-25
  • 2018-06-08
  • 2012-09-15
  • 1970-01-01
  • 2018-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多