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