我在推送到另一个 View Controller 时遇到问题。我创建了一个简单的登录系统,但问题是即使密码错误,它也会将我推送到默认 View Controller 。
import UIKit
import Parse
class ViewController: UIViewController {
@IBOutlet weak var usernameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
func createAlert(title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (alert) in
}))
self.present(alert, animated: true, completion: nil)
}
@IBAction func loginButton(_ sender: Any) {
// ************ Login Page ************
// Username error checking
if usernameTextField.text == "" {
let usernameAlert = UIAlertController(title: "Username is missing", message: "Please enter your username", preferredStyle: UIAlertControllerStyle.alert)
usernameAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
}))
self.present(usernameAlert, animated: true, completion: nil)
// Password error checking
} else {
if passwordTextField.text == "" {
let passwordAlert = UIAlertController(title: "Passwors is missing", message: "Please enter your password", preferredStyle: UIAlertControllerStyle.alert)
passwordAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
self.dismiss(animated: true, completion: nil)
}))
self.present(passwordAlert, animated: true, completion: nil) }
else {
PFUser.logInWithUsername(inBackground: usernameTextField.text!, password: passwordTextField.text!, block: { (user, error) in
if error != nil {
var displayErrorMessage = "Please try again later."
if let errorMessage = error as NSError? {
displayErrorMessage = errorMessage.userInfo["error"] as! String
}
self.createAlert(title: "Login Error", message: displayErrorMessage)
} else {
print ("Logged in")
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let cameraVC = storyBoard.instantiateViewController(withIdentifier: "CameraViewController") as! CameraViewController
self.navigationController?.pushViewController(cameraVC, animated: true)
}
})
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
注意:当用户名和密码有效时,我在调试区域收到“已登录”消息。
最佳答案
您是否在直接按钮中给出了 segue 连接,例如
so it wont check the condition if you click the button it directly segue to another VC.
如果使用的是storyboard ID则去掉VC之间的连接,可以直接访问
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let cameraVC = storyBoard.instantiateViewController(withIdentifier: "CameraViewController") as! CameraViewController
self.navigationController?.pushViewController(cameraVC, animated: true)
其他
connection oriented segue using direclty class
[self performSegueWithIdentifier:@"identifierName" sender:self];
调用类似
else {
print ("Logged in")
performSegue(withIdentifier: "identifierName", sender: self)
}
关于ios - 推送到另一个 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43014070/