2017-03-25 99 views
0

我推送到另一个视图控制器有问题。我创建了一个简单的登录系统,但问题是即使密码错误,它也会将我推向默认视图控制器。推送到另一个ViewController

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

您在storyboard中添加了segue吗?并发布你的所有代码。 – ChanWarde

+0

@Abdullah显示更多代码。 –

+0

从按钮中删除连接并将类添加到类 –

回答

0

你给出e.g

enter image description here

直接按钮SEGUE连接,因此,如果您按一下按钮直接原因请看另一个VC不会检查的条件。

删除VC之间的连接,如果您使用的是故事板ID,您可以直接访问

enter image description here

let storyBoard = UIStoryboard(name: "Main", bundle: nil) 

      let cameraVC = storyBoard.instantiateViewController(withIdentifier: "CameraViewController") as! CameraViewController 

      self.navigationController?.pushViewController(cameraVC, animated: true) 

其他

连接使用面向赛格瑞照片直接类

enter image description here

[self performSegueWithIdentifier:@"identifierName" sender:self]; 

调用,比如

else { 

      print ("Logged in") 
      performSegue(withIdentifier: "identifierName", sender: self) 

      } 
+0

工程像魅力! 注意:performSegue(withIdentifier:“identifierName”,sender:self)应该是self.performSegue(withIdentifier:“identifierName”,sender:self) 谢谢你们这么多! – Abdullah

0

如果使用赛格瑞它更好。首先用标识符为CameraViewController创建一个segue。然后,在编码中调用它。

performSegue(withIdentifier: “mySegueID”,发件人:个体经营)

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 
    } 

简单!

相关问题