Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

uinavigationcontroller - why i am not able to move from loginpage in swift?

I have Registration, Login and Home viewcontrollers

my app flow is 1) if i register with name password then i need to go home page and in home page if i tap logout i need to go login page 2) after registration if i go login with registration name and password i need to go home page and if i logout then move to home page

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

in scenedelegate:

 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 }
}

in my login viewcontroller:

@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)
}

in registration page code:

@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)

    }
        }
      }

in home page:

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

}

here i am unable to move from loginpage if i hit loginBtn or registrationBtn i am unable to move why?

please do help with code

question from:https://stackoverflow.com/questions/65922490/why-i-am-not-able-to-move-from-loginpage-in-swift

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...