Are "show" segues treated like a navigation controller? - ios

In other words, if I perform a show, I can expect:
the new view will be put on top of the stack?
once the new view unwinds, the new view will be destroyed?
if you unwind back to the initial, then all views will be destroyed except the initial view?

"Show" segue, new in iOS 8, is simply a shorthand for calling UIViewController's showViewController:sender:, which is documented here:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instm/UIViewController/showViewController:sender:
And, for UINavigationController, here: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/#//apple_ref/occ/instm/UINavigationController/showViewController:sender:
As you can see simply by reading the docs, if the view controller in question is the child of a navigation controller, and we are not in a split view controller situation, this method is identical to calling pushViewController:animated: on the navigation controller. Thus, in such a situation, the "show" segue is identical to the "push" segue from previous iOS versions.

Related

Segue from one view controller embedded within UINavigationController to another UINavigationController

I have a UINavigationController as my root controller, but then I segue from one view controller to another UINavigationController.
But when segueing from one view controller which is embedded within a UINavigationController to another UINavigationController the push segue comes from the bottom, presuming it is segueing as a popover. I tried using a show detail segue but still not luck.
Why is this occurring and how can I segue from one to the other using a push/replace segue ?
PS: Is this happening because the UINavigationControllers conflict and overrides the segue as a popover ? The reason I am using two separate navigation controllers is because the style from the previous view overrides the style of the detail view, i posted a separate question about that Cannot change style of UINavigationBar when using scrollViewDidScroll on separate View Controller
Push segue occurs only within one navigation controller. It's how it implements 'show' type segue. Making 'show' segue from navigation controller to another navigation controller is not what Xcode drawing tool considered as 'pushing'. It interprets it as popover by default.

Why use Navigation Controller as 1st item in storyboard

Sorry if this is a noob question, I'm starting to learn iOS programming.
As I see im many tutorials and examples, the 1st item in storyboard is almost always a Navigation Controller, like in the image below:
My question is: what is the reason to always put a Navigation Controller as the first view controller? Like in the image above, I think that can remove the Navigation Controller and set the Test Table View Controller as the first view controller, so why need a Navigation Controller there?
Because the navigation controller is what allows tapping a table cell to do a push segue to the next view controller.
Using Navigation Controller could let you make the push segue, It allows you to manage your views in a stack. It's OK to remove the Navigation controller, but you must use another segue to transform controllers.

How to go back to the first view

Assuming i have a setup like the above, how would one go back to the first view (eg. The one with the Label) ?
I am asking this because i am not sure if is there a particular reason why there would be multiple navigation controllers setup similar to above.
I came across this problem because popToRootViewController will only go back to the first view of that navi controller. Eg. if i was at one the views of the 2nd navigation controller and i were to run popToViewController, it will only go back to the first view controller of the 2nd navigation controller. I have since solved the problem by removing the other navigation controller but my curiosity had to be resolved.
As I stated in the comment, the second view controller is almost certainly unnecessary (but this isn't entirely necessarily the case), so your best bet is most likely to simply to remove that second navigation controller.
With that said though, how you would get back to the original view controller with the existing set up would depend largely on how you got to the current view controller.
If the second navigation controller is pushed on to the first navigation controller's navigation stack, then from any view after the second view controller, you could do something like this:
self.navigationController?.navigationController?.popToRootViewControllerAnimated(true)
Otherwise, if the second navigation controller were presented modally, you could dismiss it from any of the view controllers in its navigation stack with the following code:
self.navigationController?.dismissViewControllerAnimated(true, completion: nil)
It's important to remember that UINavigationController is a subclass of UIViewController and as such, any method we can call on the latter can also be called on the former.
One simple (Hacky) approach is to maintain a global pointer to your first navigation controller (Maybe in the AppDelegate or .pch file), and whenever you want to go your root controller, you can just say dismissViewController:animated, and then popToRootViewController.
Should work in all the cases.

iOS Storyboard Presenting Segues "relationship, embed, push, modal, custom" types

I have a basic idea what push and modal segues do. Push is used for Navigation Controller segues and Modal is the default one I've been using so far for a basic segue into another View Controller. I assume "modal" means nothing else can be going on/interrupting the segue?
Custom segues I guess are the most flexible/customizable/animatable.
I have no idea what "relationship" and "embed" segues do. Please let me know!
Thank you.
A "relationship" segue is the segue between a container view controller and its child or children -- so, the initial controller of a navigation controller, the view controllers in the tabs of a tab bar controller, and the master and detail controllers of a split view controller.
An "embed" segue is the segue between a container view and the controller that's embedded in that container view that you get automatically when you add a container view to a controller's view.
Both of these segues are executed as soon as the parent controller gets instantiated. You do not call them, but you can implement prepareForSegue, and pass information to the destination view controller.

iOS View Controller Containment Parent/Child calls dance

I am trying to make a container view controller that works similarly to navigation controller. When I add something to the stack what do I do with the view controller that is a already there?
It is still my child but I don't want it's view in the view hierarchy. Should I call removeFromParentViewController on it, and just keep a separate stack with it, in that stack? So when the view above is popped off, I can check what view I should push back in order to go back to previous one.
Or should I just remove its view, without removeFromParentViewController call, and add another child controller, and its view to container view hierarchy?
Basically what do I do with the controllers that aren't on the screen?
The "stack" is just an array that a navigation controller uses to keep track of its view controllers. If you're building your own, you will need to have an array also. The way a navigation controller works, when a controller is pushed, that controller is added to the array, and if one is popped, that one is removed from the array. When you do a transition, the one that's going off screen should call removeFromParentViewController, so it's no longer in the hierarchy (but if it's going away because of another one being pushed, you would leave it in your array -- that's how the controller knows which one to go back to on a pop). You should use transitionFromViewController:toViewController:duration:options:animations:completion: to do your transitions from one controller to the next.

Resources