导航栏在按下登录按钮后没有出现,当它移动到主视图控制器并且我已经设置了我的故事板并且把所有事情都放好了但是我觉得我在代码中被卡住了。
实际上没有segue而是编码,所以我如何进行导航工作?
用于执行导航的代码
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
self.present(vc!, animated: true, completion: nil)
@IBAction func loginAction(_ sender: Any) {
if self.emailTextField.text == "" || self.passwordTextField.text == "" {
//Alert to tell the user that there was an error because they didn't fill anything in the textfields because they didn't fill anything in
let alertController = UIAlertController(title: "Error", message: "Please enter an email and password.", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
} else {
FIRAuth.auth()?.signIn(withEmail: self.emailTextField.text!, password: self.passwordTextField.text!) { (user, error) in
if error == nil {
//Print into the console if successfully logged in
print("You have successfully logged in")
//Go to the HomeViewController if the login is sucessful
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
self.present(vc!, animated: true, completion: nil)
} else {
//Tells the user that there is an error and then gets firebase to tell them the error
let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
}
答案 0 :(得分:0)
你真的使用navigationController吗?如果是,那么它应该是pushViewController
而不是present
吗?
navigationController?.pushViewController(vc, animated: true)
答案 1 :(得分:0)
替换您的代码:
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
self.present(vc!, animated: true, completion: nil)
使用此代码:
let vc : YourViewController = self.storyboard?.instantiateViewController(withIdentifier: "Home")as! YorViewController
let navController = UINavigationController(rootViewController: vc)
self.present(navController, animated: true, completion: nil)
您只是尝试使用导航来呈现视图控制器。以编程方式导航,然后它将显示导航栏。