Dismissing and presenting View Controllers - An Explanation? - ios

Recently, I've been reading about the concepts behind dismissing and presenting View Controller. I've been able to pick up on the ideas of dismissing the previous View Controller from the destination View Controller but I can't find an answer to a few questions that have been on my mind for quite a bit.
Scenario 1: I have a login page and after the user enters their credentials, it performs a segue to another View Controller. Is it necessary to dismiss the login page afterwards or is there no reason to?
Scenario 2: I have two normal View Controllers (VC1 and VC2). If I perform a segue to VC2, will I need to dismiss VC1?
I'm mainly confused regarding the idea of when it is necessary to dismiss View Controllers and when it is not necessary to do so.
I'd appreciate it if anybody could help clear these questions up for me.

Scenario 1: After performing a segue, it switches between your view controllers [ automatically dismisses the current ViewController and presents a new one ].
So, there's no reason to dismiss the login page.
Scenario 2: No you don't need to dismiss VC1.

1) When you go from login controller to second controller you just need to present the second controller and no need to dismiss first because if you are using navigation controller as a part of your segue all the view controllers are arranged in form of a stack . So second comes on top and first goes below it.Now if you need to got from second to first you can either dismiss your controller or pop your controller.When you dismiss a controller it is not popped from stack instead just moves behind and let the first controller come on top and when you pop a controller it removes itself from stack as well.
2) Same goes for your second question no need to dismiss first when you go from first to second controller.

If there is a view controller which in most cases will be used only once (like login or settings etc.) — and especially if, after you’re done with it, it makes sense to return to the view controller you were on before — the best is to present it modally and dismiss it when you’re done. The rest of your view controllers stay in memory after the user can no longer see them, and this is expected behavior given the way Apple has created the methods for presenting and dismissing view controllers.
It is my understanding that in the Android world, this is not the case -- the default there is that when a new view controller is presented, the old one really does goes away.

As you know once user has signed in, log in screen not opens until user log out. So you should remove log in view controller from stack, it should not keep in memory. For this task, do not directly perform segue, you should change root view controller. There are lot of answers on stackoverflow for How to change root view controller?

Related

Dismissing Modal View Controller Causes Multiple VC's to Close

This question is hard for me to ask without a reference, so please see the image above. I am trying to dismiss the modal view controller on the far right of my storyboard.
The problem is when the view controller on the right hand side it dismissed and the user is returned to the middle view controller in the storyboard above, then immediately the next two view controllers are dismissed and the user is returned to the left most view controller.
I have only called the dismiss method on the right most view controller. I am wondering why this happens?
I have fiddled with the kind and presentation style of the segue to no avail. I have also tried using delegation and calling the dismiss method in the presenting view controller. This still results in the same unexpected behavior.
Any help would be great.

Know all the views presented on a view controller

I am facing a problem where I have multiple views presented at a particular time. Now if my app get timed out and I have to get back to Login Screen, I am able to pop all view controllers from the navigation stack but how to Know how many view controllers are presented at that particular moment so that I can dismiss all of them.
navigationController.viewControllers would return the current view controller stack. try that.

Dismissing View controllers and then presenting them

I have an application that has 3 view controllers.
The first one presents the second one and the second one then presents the third one. I then have a button that I want to bring me back to the first view controller. This button works. However, I try to present the 2nd view controller from the first and then the 3rd view controller from the 2nd again. Presenting the 2nd view controller works fine, but presenting the 3rd view controller doesn't work. I get the error:
Warning: Attempt to present on whose view is not in the window hierarchy!
The first view controller is the main menu to the game, the second view controller is where the game occurs and the third view controller is the end result screen. Is there a better way to do it than my way?
Does anyone have a solution? Thanks in advance.
You have to use a navigation controller. All you're doing now is pushing views in an infinite loop.
Here is a really good site to read with examples.
http://www.mysamplecode.com/2013/01/ios-navigation-view-controller-example.html

Dismissing a ViewController does not get to desired Controller

I am terrible at understanding work with interface builder, so (probably dumb) question comes.
I have created sliding view controller. When I click a cell, I want this controller slide back as it was. I used [self dismissViewControllerAnimated:YES completion:NULL];.
It used to dismiss table view controller (when I did not use Sliding). However, not it throws me not to previous controller, but to login page (initial controller). Have a look at the picture.
Hope this provides enough information. Sorry if it is not enough, as I am really bad with interface builder. I will add anything needed as requested. Thank you.
EDIT:
I have also tried to drag (holding ctrl) from sliding controller and chose ECSlidingSegue. In such case there is nothing going on.
Normal behavior of modal is when dismissed it shows its Parent Controller, in your case its I guess LoginViewController, form where you must have presented the TableViewController as modal.
However, if you wish to show a different controller other than the Parent, you can choose to push a new controller when come from TableViewController, or, you can achieve modal appearance with simple push of navigation controller, and I guess this would be easier and correct in your case.
So instead of presenting the TableViewController as modal, push it with Modal animation, and on selection of cell InboxViewController will be pushed.
There are few changes you need to do.
Create a Custom Segue from LoginController to TableViewController.
Create a push segue from TableViewController to InboxViewController.
I hope it helps.
Cheers.

navigation controller stack uses too much resources

I am building an application that have an introduction at first when users do not login.
If they log in, when navigation controller will push to Home View Controller, but the Introduction View Controller is still in the stack and use a lot of resources. How can I prevent that?
You can set the navigation controllers property to array holding ur homeview controller alone. Or set the home view controller as root of navigation controller.
You can't prevent it from being there necessarily as that is a function of how you associate your views.
If you are happy with your current transitions (UI) then you should add a bit of code after the transition has completed which gets the nav controller view controllers array, removes the first object (the login / intro VC) and saves the new array.
If you want to change the transitions then you could look at presenting the login / intro as a modal so that when it is dismissed it is automatically destroyed.

Resources