1

I have read here a lots of questions and answers regarding segues and story board , and i became even more confused about the way i build the hierarchy between views

My questions are very basic and simple, and i think they may be helpful to others :

1.In a simple words, when should i use push , modal, or costume segues ?

2.When go from view1 to view2 to view3,how should i go back from 3 to 1? can i just drag a segue from 3 back to 1? and what happens with 2 and 3 with memory aspects in that case ?

3 when dismissing a view, why it always go back to the previous view? is there a way to make a segue that dismiss automatically the previous view ?

I think a very simple and short summing on the way it works would be so helpful. thanks.

Curnelious
  • 1
  • 16
  • 76
  • 150

2 Answers2

1

If you are using Storyboards and segues you may want to take advantage of exit segues for going back. In your example you want to go from View 3 back to View 1. Depending on how you got there, you may have multiple options to go back. For example, if you used a UINavigationController and pushed your way there, if View 1 is the rootViewController you could simply call popToRootViewControllerAnimated: and it would handle what is necessary to get from wherever you're at in the navigation stack back to the root. If View 1 is not the root you could opt to use the popToViewController:animated: API instead, assuming you have a reference to View 1 from View 3 which usually isn't ideal if you're keeping concerns separated. As Renish pointed out you do have the option of accessing the viewControllers property of your navigationController.

Back to Storyboards, for better or worse, they can handle this all for you via an exit segue. The storyboard configuration determines the proper way to go back to a view that you came from. Check out this great explanation in this answer, https://stackoverflow.com/a/15839298/250190

Community
  • 1
  • 1
Chris Wagner
  • 20,773
  • 8
  • 74
  • 95
  • again you (and everyone ) always assume its a navigator. but i just can not find a formal way to use modal vc and go back 2/3/4 views – Curnelious Jan 26 '14 at 18:40
  • by the way, i do NOT want to go back to where i was- for this i can just dismiss the view . i want to go back 3 views back at once . – Curnelious Jan 26 '14 at 18:41
  • 1
    I think you need to look closer. You can go back multiple levels using exit segues regardless of it being a nav stack or modals. – Chris Wagner Jan 26 '14 at 22:52
  • Also, `dismissViewControllerAnimated:completion:` will dismiss any view controllers above it on the stack. So you can call that from view1 and view2 and view3 will be dismissed. – Chris Wagner Jan 26 '14 at 23:26
0

I think u want How to Go 3view to 1view.Simply make array of Your view controller.

How do I get the RootViewController from a pushed controller?

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/Reference/Reference.html#//apple_ref/occ/instp/UINavigationController/viewControllers

Use the viewControllers property of the UINavigationController. Example code:

// Inside another ViewController NSArray *viewControllers = self.navigationController.viewControllers; UIViewController *rootViewController = [viewControllers objectAtIndex:viewControllers.count - 2];

This is the standard way of getting the "back" view controller. The reason objectAtIndex:0 works is because the view controller you're trying to access is also the root one, if you were deeper in the navigation, the back view would not be the same as the root view.

Community
  • 1
  • 1
Renish Dadhaniya
  • 10,642
  • 2
  • 31
  • 56
  • why do you assume i am using a navigation bar ? i have a regular view controller 1 ,2 ,3 and 4. i want to go from 4 to 1. i could not find ANY formal way of doing so. – Curnelious Jan 25 '14 at 12:35