![](/img/trans.png)
[英]How to move from Viewcontroller to TabBarController without segue
[英]Segue from ViewController to first tab of TabBarController
我创建了一个标签式应用程序。 在第一个选项卡(ViewController + TableView)中,它显示了从DB获取的结果。 我有一个按钮可以打开另一个ViewController,用户可以在其中过滤结果(例如“城市”)。
现在,我如何返回带有segue的第一个标签页? 因为我需要传递参数才能对DB进行查询。 如果我在此页面和第一个ViewController之间创建一个segue,它不会显示选项卡式菜单。
感谢大伙们。
我假设你想要实现的是将数据从一个选项卡传递到另一个选项卡。
“现在,我如何返回带有segue的第一个标签页? 因为我需要传递参数才能对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!!"
}
SecondViewController:
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)
}
}
}
我希望它有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.