首页 > 解决方案 > 为什么我无法快速从登录页面移动?

问题描述

我有注册、登录和主页视图控制器

我的应用程序流程是1)如果我使用名称密码注册,那么我需要进入主页,如果我点击注销,我需要进入登录页面 2)注册后,如果我使用注册名称和密码登录,我需要去主页,如果我注销然后转到主页

 navigationcontroller(is initial viewcontroller) -> loginpage (login button or registration button) -> homepage

在场景委托中:

 func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
  
    if UserDefaults.standard.bool(forKey: "isLoggedIn") == true{
        let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc_Home = mainStoryBoard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
        window!.rootViewController = vc_Home
        window!.makeKeyAndVisible()
    }else{
        let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc_Home = mainStoryBoard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
        window!.rootViewController = vc_Home
        window!.makeKeyAndVisible()
    }
    
    guard let _ = (scene as? UIWindowScene) else { return }
}

在我的登录视图控制器中:

@IBAction func loginBtnClicked(_ sender: Any) {
    let uName = UserDefaults.standard.string(forKey: "userName")
    
    let uPassword = UserDefaults.standard.string(forKey: "userPassword")

    if userNameTextfield.text == uName && passwordTextfield.text == uPassword{
        UserDefaults.standard.set(true, forKey: "isLoggedIn")

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController

        navigationController?.pushViewController(vc, animated: true)
    }
    else{
        showAlert(title: "LogIn", message: "please enter username and password")

    }
}

@IBAction func registerBtnClicked(_ sender: Any) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "RegisterViewController") as! RegisterViewController
     navigationController?.pushViewController(vc, animated: true)
}

在注册页面代码中:

@IBAction func registerBtnClicked(_ sender: Any) {
    if firstNameTextfield.text?.isEmpty == true{
        self.showAlert(title: "Registration", message: "please enter first name")

    }
    else if lastNameTextfield.text?.isEmpty == true{
        self.showAlert(title: "Registration", message: "please enter last name")

    }else if emailTextfield.text?.isEmpty == true{
        self.showAlert(title: "Registration", message: "please enter email")

    }else if passwordTextfield.text?.isEmpty == true{
        self.showAlert(title: "Registration", message: "please enter password")

    }
    else{
        
        let fName = firstNameTextfield.text!
        let lName = lastNameTextfield.text!
        let userNameReg =  fName + " " + lName
        
                UserDefaults.standard.set(userNameReg, forKey: "userName")
                UserDefaults.standard.set(lastNameTextfield.text, forKey: "lastName")
                UserDefaults.standard.set(emailTextfield.text, forKey: "email")
                UserDefaults.standard.set(passwordTextfield.text, forKey: "userPassword")
        UserDefaults.standard.set(true, forKey: "isLoggedIn")

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
         navigationController?.pushViewController(vc, animated: true)

    }
        }
      }

在主页中:

 @IBAction func logoutBtnClicked(_ sender: Any) {
    UserDefaults.standard.set(false, forKey: "isLoggedIn")
  
    self.navigationController?.popViewController(animated: true)

}

在这里,如果我点击 loginBtn 或 registrationBtn,我无法从登录页面移动,我无法移动,为什么?

请帮忙写代码

标签: swiftuinavigationcontrollerrootviewcontroller

解决方案


推荐阅读