Kalzem
Kalzem

Reputation: 7492

How to the get UIViewController which presented a UINavigationController

I have

NAVIGATIONController A -> ViewController A -> ViewController B
                                                  |
                                                  | (modal)
                                                  | 
                                                 \ /
                                                  '
                                         NAVIGATIONController B -> ViewController C

How do I go back to ViewController A from C?

When I am in ViewController C, I tried to print the different controller values:

print(self.navigationController) //NAVIGATIONController B
print(self.navigationController?.presentingViewController) //UINavigationController (not sure what this is? It is not one of my classes)
print(self.navigationController?.presentedViewController) //nil
print(self.presentingViewController) //The same UINavigationController (still not sure...)
print(self.presentedViewController) //nil

I know how to go back to MyViewController B from MyViewController C, with this line self.navigationController?.dismissViewControllerAnimated(true, nil) but I am asking to go from C to A :-)

How do I even have access to what is before NAVIGATIONController B?

Upvotes: 3

Views: 3667

Answers (3)

xiaoming
xiaoming

Reputation: 130

in BScene's viewDidLoad:

override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.noti), name: "popAndDismiss", object: nil)
}
func noti() {
        navigationController?.popViewControllerAnimated(true)
    }

in CScene's buttonAction:

@IBAction func click(sender: AnyObject) {
    NSNotificationCenter.defaultCenter().postNotificationName("popAndDismiss", object: nil, userInfo: nil)
    navigationController?.dismissViewControllerAnimated(true, completion: nil)
}

Upvotes: 1

Aneesh
Aneesh

Reputation: 107

You can use popToViewController.

[self.navigationController popToViewController:yourviewcontroller animated:YES];

This will pop the viewcontrollers until it reach yourviewcontroller. Hope this will help.

Upvotes: 0

rmaddy
rmaddy

Reputation: 318804

The UINavigationController from self.navigationController?.presentingViewController should be your "NAVIGATIONController A".

What you should be able to do from C is get "NAVIGATIONController A" with self.navigationController?.presentingViewController and call popToRootViewController (do this unanimated).

Then dismiss "ViewController C" (and its nav controller) with self.navigationController?.dismissViewControllerAnimated(true, nil).

This way, when "ViewController C" is dismissed, the top level nav controller will already be showing "ViewController A".

Upvotes: 4

Related Questions