swift - 为什么我无法快速从登录页面移动?
问题描述
我有注册、登录和主页视图控制器
我的应用程序流程是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,我无法从登录页面移动,我无法移动,为什么?
请帮忙写代码
解决方案
推荐阅读
- php - 未使用 php mail() 函数发送的附件
- docker - Docker - 挂载目录的所有者和组
- django - 如何在向公众开放之前预热 django Web 服务?
- c - C调用Go导出函数
- kubernetes - 安全性:Yaml Bomb:用户可以通过发送 configmap 重启 kube-api
- javascript - 如何在 Java 中找到此密码规范的 javascript 等效项
- ionic3 - ionic3 文件/图像上传仅适用于模拟器而不适用于 apk
- ios - 在重叠区域点击手势事件
- css - 如何阻止角材料组件与我的粘性导航栏重叠?
- python-3.x - Mac OS 上的 pgmagick