3

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?

Kalzem
  • 7,320
  • 6
  • 54
  • 79
  • If you want to go back from one view controller to a previously displayed one, look into using "unwind segues" - that way you don't have to encode the knowledge of your presentation hierarchy into ViewController C. – Martin Ullrich May 05 '16 at 14:01
  • It is iOS9 only, I need iOS8 support. – Kalzem May 05 '16 at 14:03
  • How do I use popToView from Controller C? I can only dismiss to Controller B but then I have no control to go to A (all that from C). I don't want to create a flag on B that I set to true when presenting NavController B (and so view C) and checking on viewDidAppear if the flag is activated... It doesn't look clean. – Kalzem May 05 '16 at 14:07
  • dismiss ViewController C in ViewController B and in that completion pop ViewController B – Surya Subenthiran May 05 '16 at 14:18
  • This is my entire question actually... How do I even get access to what is before Navigation B? All my `print` show either the Navigation B or nil – Kalzem May 05 '16 at 14:21
  • 1
    @BabyAzerty unwind segues were introduced with iOS 6. only some newer API is iOS 9 specific. – Martin Ullrich May 06 '16 at 01:57

3 Answers3

4

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".

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    Thank you! It works perfectly :) Here is my full code: `(self.navigationController?.presentingViewController as! UINavigationController).popToRootViewControllerAnimated(false)` `self.navigationController?.dismissViewControllerAnimated(true, completion: nil)` – Kalzem May 05 '16 at 14:40
  • Great Answer ! and Thanks for posting your implemented answer too – Naishta Apr 30 '18 at 20:08
1

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)
}
xiaoming
  • 130
  • 9
0

You can use popToViewController.

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

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

Aneesh
  • 107
  • 7