我在访问位于TAB和NAV控制器后面的视图控制器时遇到问题。
我可以访问第一个VC(参见下面的代码),但我无法访问第四个VC。
SenderVC----->TabBarVC-⎜----NavVC----FirstVC
⎜----NavVC----SecondVC
⎜----NavVC----ThirdVC
⎜----NavVC----FourthVC
我错了什么?
如果有人能指出我正确的方向,那就太好了。谢谢。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "segueVC0") {
let tabVC = segue.destination as! UITabBarController
let navVC = tabVC.viewControllers![0] as! UINavigationController
let destVC = navVC.viewControllers[0] as! FirstVC // ==> this transition is working
} else if (segue.identifier == "segueVC4") {
let tabVC = segue.destination as! UITabBarController
let navVC = tabVC.viewControllers![4] as! UINavigationController
let destVC = navVC.viewControllers[0] as! FourthVC // ==> this transition is NOT working !!!
} else {
print ("wrong segue ID")
}
}
答案 0 :(得分:1)
您好为UITabBarController创建自定义类,然后像这样获取目标
let tabVC = segue.destination as! HomeTabBarcController
let navVC = tabVC.viewControllers![4] as! UINavigationController
let destVC = navVC.topViewController as! FourthVC
它对我有用。希望它对你有用
答案 1 :(得分:1)
您好我在这里获得了第四个VC的实例
类ViewController:UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func buttonTapped(sender: AnyObject) {
print("ViewController - buttonTapped()")
performSegue(withIdentifier: "seg4", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "seg1" {
let tabVC = segue.destination as! UITabBarController
let navVC = tabVC.viewControllers![0] as! UINavigationController
let destVC = navVC.viewControllers[0] as! Seq1 // ==> this transition is working
print(destVC)
}
else if segue.identifier == "seg4"{
let tabVC = segue.destination as! UITabBarController
let navVC = tabVC.viewControllers![3] as! UINavigationController
let destVC = navVC.viewControllers[0] as! Seq4 // ==> this transition is working
destVC.hello()
print(destVC)
}
}
}
class Seq1:UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
class Seq2:UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
} class Seq3:UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
} class Seq4:UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print("Hello")
// Do any additional setup after loading the view, typically from a nib.
}
func hello() {
print("Hello")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
注意:标签栏控制器将始终显示FirstVC。因为它是默认选择。如果需要,请更改selectedIndex = 3