![](/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.