Different views based on user type - Correct segue type after login? - ios

I have a Login View Controller which is the initial view controller. Here users input their username and password, which is then sent off to the server to be checked. If the user has inputted correct credentials the server responds with some JSON data including the user type (either 'student' or 'demonstrator'). I'm using the returned user type to decide on what views the user the will see. (users will see different tab controllers)
The bit I'm struggling on is on what type of segue to perform. Push or Modal? I tried embedding a navigation controller into the Login View Controller and performing a Push Segue to the correct view, but this seems to not play nice with other navigation controllers that are embedded in the other views (title's disappear etc).
Is the modal segue type the correct one to use in this situation? The user wont be returning back to the login screen at all.

Keep in mind that you CANNOT nest navigation controllers. Pushing a navigation controller onto the navigation stack leads to the types of errors you saw with missing titles and the like. Always modally segue to a navigation controller, and use unwind segues to get back if you need to.
A push segue is used exclusively to push the destination view controller onto the navigation stack. The initial view controller must be in a navigation stack before you can push segue. If your login view is the root view of a navigation controller, then pushing the next view is the way to go.
On the other hand, which it sounds like may be your case, if your login view is your initial view and you want to segue to two different navigation controllers containing different paths for different user roles, you'll want to modally segue to the navigation controllers themselves.
Some Reference Material From Apple:
https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/UsingViewControllersinYourApplication/UsingViewControllersinYourApplication.html#//apple_ref/doc/uid/TP40007457-CH6-SW1

Related

Changing the number of View Controllers affected by Navigation Controller

So I have a navigation controller that is at the beginning of a series of view controllers. The series consists of 5 view controllers, but I want the navigation controller to only use the first 4 view controllers. Im not sure if there is a way to change the relationship between view controller 4 and view controller 5 so that view controller 5 and the rest of the app are not affected by the navigation controller.
Im not sure if there is a way to change the relationship between view controller 4 and view controller 5 so that view controller 5 and the rest of the app are not affected by the navigation controller.
There are a number of things that you could do:
Remove the navigation controller entirely and make your "view controller 5" the window's root view controller.
Set the navigation controller's array of view controllers to an array that contains only "view controller 5." This would effectively make that view controller the navigation controller's root controller, and from there you could just never push another controller onto the navigation stack.
Keep the current relationship, but hide the navigation bar and prevent the user from going back to "view controller 4."
Rethink your user interface. For example, if your first four view controllers are meant to lead the user through some set of initial questions, a login procedure, etc., then you could make "view controller 5" the app's main view controller, and present the navigation controller containing the controllers 1-4 modally.
Of those, and without knowing what you're actually up to, I believe reconsidering your UI is probably the best plan. Users should generally be in control of your app instead of the other way around, and they should never wonder why they can't get back to some part of the app that they've seen before. Also, it's poor form to break behaviors that users have learned to expect, and changing the user's ability to navigate through a series of view controllers would be a prime example. Using a modal presentation of the navigation controller with the first four view controllers should be fine, though.

Dismissing and presenting View Controllers - An Explanation?

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?

Benefits from using UiNavigationController

I am developing an iOS app that I have already developed for Android.
The problem is I don't know how to organize my UIViewControllers considering the following scheme of my app pages:
The scheme is simple: there is a login page which leads to the main page. From this main page, there are four buttons which all lead to a specific view hierarchy but at the very bottom of each, the user will be able to go back directly to the main page. Each page accessed by the main page will also have a custom back button (an image of my own)
The question is: is there any benefit in using a UINavigationController (obviously with the main page as its root) in my case? Or can I simply create each Controller and using only Modal Segues?
If your view controllers have a navigation relationship so using UINavigationController is the way to go:
In 'push' segue, you are basically pushing your ViewController into an
already setup "navigation stack". Well, of course, this is under the
assumption that the ViewController that performs the 'pushing'
operation belongs to the same navigation stack as the ViewController
is pushed into. Generally, you push a ViewController if the pushed
ViewController has some sort of a relationship with the pushing
ViewController. This is very common in applications that has a
NavigationController in its system. A good example for a push segue is
a system where you are displaying a list of contacts. And on tap of a
particular contact, you are pushing a VC that has the corresponding
details of the contact.
Example is real world: list of products => product details => product reviews
If you want to temporary present a view controller and the main focus is your view controller but you need to present another view controller to perform a task like "filter" , "login", adjust "settings" then modal segue is the way to go
In 'modal' segue, there is no stack as such. You are presenting a VC
'modally' over the presentee VC, if that makes sense. This can happen
across any ViewController without any relationship rules. The
presenter should take care of dismissing the VC it presented. A good
example for modal segue is login. On tap of login, you are modally
presenting a VC that has no relationship with the presenter.
If your view controllers are not related to each other, but each view controller has his own navigation stack then UITabBarController is the way to go
Storyboards (Xcode): What is the difference between a push and modal segue?
I would say if each of the additional view controllers from the main "home" view controller don't have any children view controllers, then you can just have each button present a view controller modally.
The main difference is if you are using a navigation controller, you can "pushing" a vc onto the navigation stack of view controllers, whereas presenting it modally can be thought of a "one time" action where the user does something on the new screen and has no where to advance to logically (like adding information to a new contact).
You can see this post for a more detailed answer:
What is the difference between Modal and Push segue in Storyboards?
Deciding whether to use a Modal segue vs a Show (push) depends entirely on purpose and context of the user's experience. If you are leading the user down a path which is linear, where each successive VC is diving deeper in to a singular idea, then use Show segues and NavigationControllers. Examples include, Settings app, where you can drill into all the specifics. Most e-commerce app will use a NavigationController to lead the user through a purchase.
If you want to present the user with a single concept, which the user can respond to, or close it to continue using the rest of the app. Then use a modal presentation. Adding a contact in the iPhone is a fine example of this.
Visually, the difference is that a Show segue presents the VC from the right side of the app, sliding onto the previous VC. (If the user has Arabic language turned on, a right to left language, the Show segue will come from the left hand side of the VC) A modal comes from the bottom of the app.
From looking at your drawing, but not know anything else about your app, I think you want to use NavigationControllers. You may also want to consider a TabBarController. If each of these buttons lead the user on various ways of using the app, like mini apps within one big one, then a TabBarController is appropriate.

how to make a page accessible from every other page in my app and able to unwind to where the segue comes from

I am trying to create an on-top structure where user can go to a general page G from any other pages with a special gesture and go back to the page where s/he comes from.
Is there a way to do this while not implementing a segue and an unwind segue from every other page to page G?
Thank you!
Yes, you can do this without having a segue from each controller, but it depends on how your controller hierarchy is set up. If for instance, you have a navigation controller, and all your view controllers are pushed onto its stack, you can create one segue from the navigation controller to the one you want to go to from any of your other view controllers. You could also do this if your other controllers are embedded in a tab bar controller (the tab bar controller would present the common controller).

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