0

I have four viewcontrollers A , B , C and D

i go from A to B to C to D

 let newViewContoller: B = self.storyboard?.instantiateViewControllerWithIdentifier("B") as! B    
 self.presentViewController(newViewContoller, animated: true, completion:nil)

To go back from D to A

I am using the following

self.view.window!.rootViewController?.dismissViewControllerAnimated(true, completion: nil)

The problem that I am facing is, viewDidAppear of viewController B and viewController C is being called.

I do not know why is that happening? What is the right way of going back to initial UIViewController.

Cloy
  • 2,141
  • 23
  • 32
  • search for unwind segue – pacification Aug 22 '17 at 14:00
  • Check the solution from this question [Dismiss current view with closing other view](https://stackoverflow.com/questions/31952948/how-to-dismiss-the-current-viewcontroller-and-go-to-another-view-in-swift) – cole Aug 22 '17 at 14:04
  • Are you presenting your B, C and D controllers using pushController? In that case you can use from D navigationController?.popToRootViewController(animated: true, completion: nil) – Fran Martin Aug 22 '17 at 14:14
  • *"i go from A to B to C to D"* ??? What does that mean? How do you "go from ... to"? Are you using a Navigation Controller? A TabBarController? Are you using presentViewController()? – DonMag Aug 22 '17 at 14:18
  • @DonMag I am using presentViewCOntroller – Cloy Aug 22 '17 at 14:21
  • Try to replace self.view.window with (UIApplication.shared.delegate as! AppDelegate).window – Prashant Tukadiya Aug 22 '17 at 14:24
  • "The problem that I am facing is, viewDidAppear of viewController B and viewController C is being called." That is not a problem. It is correct behavior. – matt Aug 22 '17 at 14:24
  • @Cloy - so, from `A` you `presentViewController()` to `B`? and then again `presentViewController()` from `B` to `C`? and again from `C` to `D`? You may want to re-think how you are navigating between view controllers. – DonMag Aug 22 '17 at 14:25
  • @DonMag I am new to IOS so Is it wrong to to presentViewController from A to B then to C and D. I am doing so because i have a lot of view contollers in my project. And i have a logout button in all view controllers. – Cloy Aug 22 '17 at 14:26
  • @matt I am pretty new to iphone development and you have the right knowledge .My problems is there are a lot of viewcontrollers in my application. And i have a logout button in all view controllers. So how should i present my view controllers and call logout. – Cloy Aug 22 '17 at 14:31
  • But that is not what you asked. You asked about dismissing view controllers, and they are being dismissed perfectly. – matt Aug 22 '17 at 14:41
  • @matt I tried to ask the question in as simple as i could. I know i do not have understanding of a lot of concepts in IOS. I am a fan of Oriely books. Its great to know that i am talking to one of the authors. I will surely read your book. Can you tell me what is wrong? What do i not know? Is there any other proper solution? – Cloy Aug 22 '17 at 14:47
  • @Cloy - it's impossible to say what's the best way to structure your app's navigation. You say you *"... have a lot of view controllers ..."*. Well, what does that mean? Is there a logical "flow" to the views in your app? Is it random views that just show up? do you have various "sections" in your app? Will the user sometimes want to go `A -> B -> C -> D -> C -> B -> A`? Or only `A -> B -> C -> D -> A`? We can't help you with the amount of information you've given. My advice: figure out the design of your app, then start building it. Then come back with specific questions (if you have any). – DonMag Aug 22 '17 at 15:15

2 Answers2

1

Your ViewControllers A, B, C, and D are in a stack data structure which implement last in, first out. This means that to get to ViewController A by dismissing ViewControllers it will pop ViewControllers D, C, B in this order. In this case ViewControllers C and B which are popped from the stack will be the top most ViewController for a split second, which will fire the viewWillAppear() and viewWillDissapear() functions. Apple documentation:

calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack.

Instead what you should do if you have a navigation controller and do not want to have C and B appear is to popToRootViewController() from the currently displaying viewController (D), which will pop the current and then dealloc C and B and then show A.

0

From what I know, dismissing 4 controllers at once is not possible, but there's a tricky way to do--

Create a segue from Viewcontroller D to viewcontroller A, call it ForceSegue, then you should have something on the A controller to perform this action, if it's an action paste the following.. Objective-c

[self performSegueWithIdentifier:@"ForceSegue" sender:self];

Swift 3

performSegue(withIdentifier: "ForceSegue", sender: self)

Just a tricky way, Hope this helps!

Caplin YT
  • 269
  • 1
  • 5
  • 16