Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

222
Vistas
when need to switch the rootViewController

I've been working on a Swift project and I have two view controllers, the login view controller & the home view controller. When a user launches the app, I want to display the login view controller if the user is not logged in, on the other hand, if the user is logged in, I want to display the home view controller.

So the flow is gonna be something like this.

When the user is not logged in, display

  1. LoginViewController
  2. HomeViewController

When the user is already logged in, display

  1. HomeViewController

In the scene delegate, I've written

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let scene = (scene as? UIWindowScene) else { return }

    window = UIWindow(frame: scene.coordinateSpace.bounds)
    window?.windowScene = scene
    window?.rootViewController = HomeViewController() or LoginViewController() depending on the user's login status
    window?.makeKeyAndVisible()
}

I was wondering if I should apply the HomeViewController as a rootviewcontroller regardless of the user's login status (and maybe present loginVC on the homeVC when the user is not logged in), or I should switch the view controller depending on the user's login status.

So, in this case, what is the point of switching rootviewcontroller? and why it is (or isn't important) to switch the root view controller?

Is there anything I should consider when I apply view controller to the root viewcontroller property?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

I think there can be another cases, like RootVC is a container ViewContoller consists of HomeVC and LoginVC. example)

final class RootVC: UIViewController {
 private let loginVC = LoginVC()
 private let homeVC = HomeVC()
 
 override func viewDidLoad() {
  super.viewDidLoad()
   addChild(homeVC)
   view.addSubview(homeVC.view)
   addChild(loginVC)
   view.addSubview(loginVC.view)
 }

 func showVC() {
  if isLogin {
   homeVC.hide()
   loginVC.show()
  } else {
   reverse()
  }
 }
}
over 4 years ago · Santiago Trujillo Denunciar

0

// SceneDelegate.swift
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
 // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let _ = (scene as? UIWindowScene) else { return }
// if user is logged in before
 if let loggedUsername = UserDefaults.standard.string(forKey: "username") {
window?.rootViewController = HomeViewController()
 } else {
// if user isn't logged in
window?.rootViewController = LoginViewController()
 }
 }
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda