SideMenu Navigation Control

Hi guys, I followed the Firebase Authentication tutorial to create a sign up and login page, but I’m trying to do a side menu in my home screen. So I used another navigation controller on my home screen but whenever I login into my app, I can’t see the side menu button at all. How am I supposed to show the navigation controller on the home screen? Am I Supposed to embed the very first view controller with the side menu navigation controller? Then how do I hide/show the navigation controller for login and signup and the navigation controller for the side menu at the appropriate times?

Thanks for any feedback!

Hi @Gryffin,

Can you provide a screen shot of your storyboard layout.

The reason I am asking is that for this to work and have a Navigation bar visible at the top you need to embed the HomeViewController in a Navigation Controller.

When you then transition from the Login screen or the SignUp screen you need to have code similar to this:

func transitionToHome() {
    let homeVC = storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController
    view.window?.rootViewController = UINavigationController(rootViewController: homeVC!)
    view.window?.makeKeyAndVisible()
}

Screen Shot 2020-08-07 at 7.39.15 AM

@Gryffin

What does your function transitionToHome() code look like in both Login and SignUp ViewControllers?

Hi, Ii can’t show you my original code because I just changed it, but unfortunately, the code you suggested isn’t working still. The side menu still isn’t showing up.

If I could also ask two more questions that I’ve been struggling with, I greatly appreciate it. If I’m setting user defaults, do I set them before the transition to the home screen or not? Because what’s happening in my user defaults don’t seem to be working.

For example, here’s my code for setting user defaults are someone logs in/signs up:

UserDefaults.standard.set(1, forKey: "Key")

And then here’s my user defaults that I set in Scene Delegate:

         guard let _ = (scene as? UIWindowScene) else { return }


    if UserDefaults.standard.integer(forKey: "Key") == 1{
              // user logged in
         
        let homeController = HomeViewController()

              self.view?.window?.rootViewController = homeController // change rootViewController to HomeController
              self.view?.window?.makeKeyAndVisible() // show window
          }
          else{
            // Create the root view controller as needed
            let vc = ViewController()
             _ = UINavigationController(rootViewController: vc)

    }
} 

Another Question: As you can see from the code, it doesn’t have the navigation controller embedded with the home controller in it because scene delegate doesn’t support storyboard. How can I do that navigation when I manually set the root view controller in Scenedelegate based on the UserDefaults?

Thanks!

@Gryffin

Sorry about the delay in responding.

The method you use to transistion is really important. What you have to do is instantiate the window rootViewController as a UINavigationController and that in itself has the rootViewController as the HomeViewController.

That ties the two together and ensures that you have a Navigation Bar at the top.

I assume that you are modelling this App on the CustomLoginDemo that Chris Ching set up in his video.

If your intention is to have the same SignUp or Login process then that code should be the same. What is different is the menu you want to set up in the HomeViewController which I assume you want to trigger through a button on the Navigation Bar. Is that correct?

Yes, thank you! I just followed along with the video for the starting point and then started implementing the functionality on the home page

OK so your Login and SignUp should be as per the video so that your authentication works correctly. What were you wanting to achieve with UserDefaults?

I closed Xcode and tried again with the code. It seems to be working now, thanks so much! For the user defaults, I wanted to save their logged in state so I set the value to 1 (after successfully logging in or signing up) and then checked for it while the app was setting up in scene delegate, that way if it comes from being inactive the user doesn’t have to sign in again and again.

You could achieve that check for a user already logged in through ViewController where you choose Login or SignUp rather than putting code in to SceneDelegate.

If you create a function in ViewController like checkUserLoggedIn() and then call that from viewDidLoad()

In that function interrogate UserDefaults to see if the user is already logged in and if so, do the same transition to HomeViewController. If the user has not logged in then fall through that function and allow the user to Login or SignUp.

In fact, from memory that was a challenge that Chris Ching suggested as an exercise beyond the scope of the tutorial.

To be honest with you SceneDelegate is really for more fundamental setup code rather than decision making depending on whether a user is logged in or not. At least that’s my view.

Oh ok. I just moved the code to the View Controller but it’s still not working - I tested it on my phone as well. Any ideas what’s going wrong? I did the same transition you suggested with the root view controller but its not switching to home when the user already logged in.

Edit:I tried some other things too by setting it to true and stuff but it’s still not working. This time I honestly have no idea what’s wrong.