Search code examples
iosswiftswiftuistoryboard

Swift - Redirect from StoryBoard to another


`Starting the project, I wanted to be neat when it came to having the views, so I made a storyBoard where I have my LogIn view and a StoryBoard that has a home, that is, I don't work with a Main as a storyBoard.

When I start the application from the sceneDelegate I can know if I am logged in or not since I saved the corresponding flag in my UserDefaults, so this is how I create a storyBoard or another with its respective UIViewController.

My problem is when I do the LogIn per se, since at that point I would like to redirect to the homeStoryBoard, which uses the HomeUIViewController. By having these views organized separately, I am not finding a way to go to that view without the possibility of returning to logIn, since I have already logged in. The way I found, raises the view from home as if it were a popUp but that would not be working for me.

I came to make a new project where I used a single storyBoard Main and have my views there but with this solution the problem I see is that the "Back" button is generated that would make me return from Home to LogIn and that would not help me either.

I made the next code in mi sceneDelegate...

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        if let _ = UserDefaults.standard.string(forKey: kIsLogged) {
            print("I'm loggin")
            guard let windowScene = (scene as? UIWindowScene) else { return }
            
            window = UIWindow(frame: windowScene.coordinateSpace.bounds)
            window?.windowScene = windowScene
            window?.makeKeyAndVisible()
            
            let storyboard = UIStoryboard(name: "ServiceHomeViewStoryBoard", bundle: nil)
            window?.rootViewController =  storyboard.instantiateViewController(withIdentifier: "SB_ServiceHome")
        } else {
            print("I'm not loggin")
            guard let windowScene = (scene as? UIWindowScene) else { return }
            
            window = UIWindow(frame: windowScene.coordinateSpace.bounds)
            window?.windowScene = windowScene
            window?.makeKeyAndVisible()
            
            
            let storyboard = UIStoryboard(name: "LogIn", bundle: nil)
            window?.rootViewController =  storyboard.instantiateViewController(withIdentifier: "LogInStoryBoard")
        }
    }

In my LooginViewController I made after logIn function success...

let storyBoard = UIStoryboard(name: "ServiceHomeViewStoryBoard", bundle: nil)
            let serviceHomeViewController = storyBoard.instantiateViewController(withIdentifier: "SB_ServiceHome")
            self.navigationController?.pushViewController(serviceHomeViewController, animated: true)
            self.navigationController?.present(serviceHomeViewController, animated: true)
            self.present(serviceHomeViewController, animated:true, completion:nil)

The second view appears as a modal


Solution

  • i think you need to set root view controller while moving to home controller. here i have extension for scenedelegate where you can root view controller.

    Extension

    In this Extension set navigation bar isNavigationBarHidden set as per your requirement.

    extension SceneDelegate {
        func setRootViewController(controller:UIViewController) {
            let navigationController = UINavigationController(rootViewController: controller)
            navigationController.isNavigationBarHidden = true
            self.window?.rootViewController = navigationController
            self.window?.makeKeyAndVisible()
        }
    }
    

    How to Use

    guard let scene = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate else {return}
    let storyBoard = UIStoryboard(name: "YOUR_STORYBOARD_NAME", bundle: nil)
    guard let vc = storyBoard.instantiateViewController(withIdentifier: "YOUR_CONTROLLER_IDENTIFIER") as? YOUR_CONTROLLER_NAME_CLASS else {return}
    scene.setRootViewController(controller: vc)