2017-03-09 68 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

相關問題