2
votes

I'm developing an app (in Swift with UI builder) where I have a UITableViewController embedded in a UINavigationController so I can present details about the rows, use a standard '+' button to add rows to the table etc. At that point, the UINavigationController is the entry point to the storyboard.

Now I want to add a login screen as a UIViewController before the UITableViewController in the storyboard, so the user needs to log in first and, upon successful authentication, would be taken to the table view. I'm expecting the new view controller should be embedded within the existing navigation controller (makes sense to me for the sake of continuity). However, I can't figure out how I can insert the login screen within the navigation controller without breaking the existing navigation. I've tried several approaches, e.g. creating a new segue from the login screen to the table view, then replacing the relationship "root view controller" between the navigation controller and the table view with a new one with the login screen, but when I do this all the navigation elements in the table view and the subsequent screens (details, data creation) disappear.

Is there a trick to doing this, or will I have to recreate the whole navigation logic?

Thanks!

2
How are you intending to navigate from your Login screen to your Table View?DonMag
Don't insert the login screen. Present it from the top view so the user doesn't see the top view until after successfully logging in.rmaddy
@DonMag My attempt was to instantiate it using the storyboard in the login screen class, then present it upon pressing a "Login" button and checking for the proper credentials. Doing this, the table view is displayed, but, as I said, the navigation bar at the top of the screen has disappeared above the table view.David
@maddy Is that the standard way to do it? Not quite sure, since I want to also allow the user to e.g. go to an account-creation screen from the login screen, or display user info etc after login and before presenting the table, it feels like the login screen should be a rightful step in the navigation process, which may be skipped after the user has successfully logged in once.David

2 Answers

2
votes

Sounds like you're on the right track... not sure where you're going wrong.

This would be (one) valid approach, and should give you what you're going for.

Edit: Note also, if you are navigating via code, you want to PUSH ViewControllers onto the Navigation stack, not "present" them. E.G.: self.navigationController?.pushViewController(tableViewController, animated: true)

Start with NavController... TableView is Nav's Root, and DetailView is shown on Table Row Select:

enter image description here

Delete the "Root VC" connection, and drop a new ViewController in-between - should look about like this now:

enter image description here

TableView and DetailView are missing the NavBar at the top - that's OK.

Now, connect the LoginView as Nav's Root, and connect a Show segue from Login Button to TablView:

enter image description here

If all goes according to plan, NavBar is again showing at the top of TableView and DetailView, as desired.

0
votes

I had the same problem You should first add a new view and connect the navigation controller and the new view using control and trackpad. Then choose the Root View Controller option and that'll create a new root view and then you can choose the next screen normally. Do accordingly see the image and follow.

Select the root view controller option:

Hope this helps.