2017-03-09 66 views
-1

访问坐在TAB和NAV控制器后面的视图控制器时遇到问题。执行segue查看控制器后面的选项卡和导航控制器

我能够访问第一个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

试试这样navVC.topviewcontrolle as! FourthVC –

回答

1

嗨在这里工作,我得到第四VC

类视图控制器的实例:的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) 
    } 

} 

}

类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. 
} 

}

类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. 
} 

} 类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. 
} 

} 类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

+0

谢谢你的回答。但它不起作用。调用seg4中的函数Hello,但转换进入控制器1(尽管在代码索引3中设置了索引0)。任何消化? PS:故事板中的段落从ViewController转到TabBar控制器(seg1)和ViewController转换为TabBar控制器(seg4)。可以向我发送我的项目吗? – Koen

0

嗨创建的UITabBarController自定义类,然后让目标这样

let tabVC = segue.destination as! HomeTabBarcController 
    let navVC = tabVC.viewControllers![4] as! UINavigationController 
    let destVC = navVC.topViewController as! FourthVC 

这对我的作品。希望它会为你

+0

NITHI CHAN,Nazmul Hasan,感谢您的反馈,但它并不是在鼓励。也许我没有正确解释设置。请看看我的问题描述。我添加了一些文字,更好地解释我的设置。谢谢你的建议。 – Koen

相关问题