我有推送到另一个视图控制器的问题。我创建了一个简单的登录系统但问题是,即使密码错误,它也会将我推送到默认的视图控制器。
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.
}
}
注意:当用户名和密码有效时,我在调试区域中显示“登录”消息。
答案 0 :(得分:0)
你是否在直接按钮中给出了segue连接,例如
因此如果单击按钮直接转到另一个VC,它就不会检查条件。
如果您使用的是故事板ID,则删除VC之间的连接,您可以直接访问
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let cameraVC = storyBoard.instantiateViewController(withIdentifier: "CameraViewController") as! CameraViewController
self.navigationController?.pushViewController(cameraVC, animated: true)
否则
使用direclty类
的面向连接的segue
[self performSegueWithIdentifier:@"identifierName" sender:self];
像
一样打电话else {
print ("Logged in")
performSegue(withIdentifier: "identifierName", sender: self)
}
答案 1 :(得分:0)
如果你使用segue会更好。首先使用标识符为CameraViewController创建一个segue。然后,在编码中调用它。
performSegue(withIdentifier:“mySegueID”,sender:self)
答案 2 :(得分:0)
1)从故事板中简单地从IBAction目标中删除您的代码。
2)在故事板中选择你的segue - >属性 - >标识符 - >在这里设置一个标识符
3)在您的代码中添加此委托。
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
if identifier == "youridentifier" {
if username and password correct
{
return true
}
else
{
show alert here
return
}
}
return true
}
简单!