UIViewController无法导航到swift中的另一个页面

时间:2016-01-25 19:55:23

标签: ios swift uiviewcontroller segue viewcontroller

我之前写过这个函数,它运行正常。经过一周的休息后,我再次开始研究这个项目,同样的代码无效。我在这里错过了什么吗?基本上如果按下按钮,checkCredentials将会运行。 "其他"中的所有内容部分工作正常,但if user != nil部分不起作用:

func checkCredentials (){
  PFUser.logInWithUsernameInBackground(usernameLoginTxt.text!, password: passwordLoginTxt.text!){
    (user:PFUser?, error:NSError?) -> Void in
    if user != nil{


        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc: UIViewController = storyboard.instantiateViewControllerWithIdentifier("SearchViewController") as UIViewController
        self.presentViewController(vc, animated: false, completion:nil)
        NSLog("it was successfull dude")

    }else{

        let title = "Warning!"
        let message = "You have entered the wrong username or password.\n Please try again!"
        let okButton = "Ok"
        let alert = UIAlertController(title: title, message: message, preferredStyle:  UIAlertControllerStyle.Alert)
        let okayButton = UIAlertAction(title: okButton, style:  UIAlertActionStyle.Cancel, handler:  nil)
        alert.addAction(okayButton)

        self.presentViewController(alert, animated: true, completion: nil)
    }
  }
}

1 个答案:

答案 0 :(得分:0)

这对你有用......

   func login(){
      let user = PFUser()
      user.username = userNameText.text
      user.password = passwordText.text

  PFUser.logInWithUsernameInBackground(userNameText.text!, password: passwordText.text!, block: {
        (user: PFUser?, Error: NSError?) -> Void in

        if Error == nil {

            dispatch_async(dispatch_get_main_queue()) {
                let Storyboard = UIStoryboard(name: "Main", bundle: nil)
                let AfterLoginVC: UIViewController = Storyboard.instantiateViewControllerWithIdentifier("AfterLoginVC") as! UINavigationController
                self.presentViewController(AfterLoginVC, animated: true, completion: nil)
            }

        } else {
    let title = "Warning!"
    let message = "You have entered the wrong username or password.\n Please try again!"
    let okButton = "Ok"
    let alert = UIAlertController(title: title, message: message, preferredStyle:  UIAlertControllerStyle.Alert)
    let okayButton = UIAlertAction(title: okButton, style:  UIAlertActionStyle.Cancel, handler:  nil)
    alert.addAction(okayButton)

    self.presentViewController(alert, animated: true, completion: nil)
        }
    })
 }

另外请确保在上周你没有意外更改你的Parse客户端密钥(之前我已经完成了),这将导致它不显示任何错误,只是不起作用。