[英]How can I do this in Swift? (My version don't working)
我在加入導航器 controller 和 containerView 時遇到問題。 我想如果我單擊 ViewController 中的按鈕,TabViewController 中的視圖必須在其他視圖上更改。 (我知道,沒有代碼更改視圖,但我想先打印此日志。) TabBarViewController 嵌入在 ContainerView 中。 TabViewController 是 rootView NavigationController。
所以我創建了這個:
這不起作用,當單擊按鈕時,我在日志中看不到“這是測試”文本。
這是我的 swift 代碼:
import UIKit
class TabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("this is test");
}
}
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func click1(_ sender: Any) {
navigationController?.performSegue(withIdentifier: "segue1", sender: self)
}
}
如果我的問題是正確的,您想在單擊 ViewController 中的按鈕時更改 TabBarController 的索引。 如果我錯了,請糾正我。
給連接ViewController
和TabBarController
的 segue 一個標識符。
然后在你的ViewController
中,聲明這個
var yourTabBarController : UITabBarController?
然后在ViewController
的prepareForSegue
方法中,
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "yourTabBarControllerSegue" {
if let tabBarController = segue.destination as? UITabBarController {
self.yourTabBarController = tabBarController
}
}
}
最后,按下按鈕:
@IBAction func yourButtonPressed(_ sender : Any) {
if yourTabBarController?.selectedIndex == 0 {
yourTabBarController?.selectedIndex = 1
} else {
yourTabBarController?.selectedIndex = 0
}
}
您嘗試執行performSegue
的ViewController
似乎沒有navigationController
。 在沒有navigationController
的情況下嘗試performSegue
,如下所示:
@IBAction func click1(_ sender: Any) {
performSegue(withIdentifier: "segue1", sender: nil)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.