Max
Max

Reputation: 15965

Segue between navigation controllers doesn't seem to push the stack, but back button works

I have a segue between two navigation controllers (A -> B). Using the buttons, I am able to navigate between these controllers seamlessly. The issue is that in controller B, I would like to access controller A, which is lower on the stack. I was trying this:

self.navigationController!.viewControllers[0] as! A

This gives me an error because it says I cannot cast B to A. Inspecting the size of self.navigationController!.viewControllers shows that it has count 1. Where did A go on the stack?

The segue type is "Show (e.g. Push)"

Upvotes: 0

Views: 231

Answers (1)

jrc
jrc

Reputation: 21921

I'm guessing you don't really have a segue between two navigation controllers. If your app is a standard master-detail application, you actually have a segue between A.rootViewControllerB (where A and B are the navigation controllers), and that the code you're referring to is in B.rootViewController (e.g. "DetailViewController"), not B.

As such, self is B.rootViewController, self.navigationController is B, and so self.navigationController!.viewControllers[0] is B.rootViewController, not A.rootViewController as you expect.

Try this:

    let A = self.navigationController!.parentViewController! as! UINavigationController
    NSLog("%@", A.viewControllers[0])

Upvotes: 2

Related Questions