Koen
Koen

Reputation: 71

perform segue to view controller behind tab and nav-controller

I have issues accessing a view controller that is sitting behind a TAB and NAV controller.

I'm able to access the first VC (see code below) but I can not access the fourth VC.

         SenderVC----->TabBarVC-⎜----NavVC----FirstVC
                                ⎜----NavVC----SecondVC
                                ⎜----NavVC----ThirdVC
                                ⎜----NavVC----FourthVC

What do I wrong?

It would be great if someone could point me in the right direction. Thanks.

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")
    }
}

Upvotes: 0

Views: 392

Answers (2)

NITHI CHAN
NITHI CHAN

Reputation: 56

Hi here i got the instance of Fourth VC

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

}

Note: The tabbar controller will always shows FirstVC. Because it is the default selection. If you want, change the selectedIndex = 3

Upvotes: 2

NITHI CHAN
NITHI CHAN

Reputation: 56

Hi Create custom class for UITabBarController and then get the destination like this

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

It works for me. Hope it will work for you

Upvotes: 1

Related Questions