2016-01-25 42 views
0

我以前寫過這個功能,它工作正常。休息一週後,我開始重新研究這個項目,並且相同的代碼無法工作。我在這裏錯過了什麼嗎?基本上如果一個按鈕被按下,checkCredentials將會運行。在「其他」一節中的一切工作完全正常,但if user != nil部分不工作:UIViewController無法在swift中導航到另一頁

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

'error'中是否有值?聽起來像是什麼東西可能出錯解析請求 –

+0

你能澄清,如果你看到NSLog的消息?如果不是,那麼用戶== nil和你的代碼不被執行,或者整個完成處理程序不被執行(可能是由於響應中的錯誤)。事實上,後者會解釋爲什麼它在一週前工作,現在不工作:) –

回答

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) 
     } 
    }) 
} 

還可以肯定的是,在過去一週,你沒有意外地改變你的解析客戶端密鑰(我之前做過),這將導致它不顯示任何錯誤,但只是不工作。

相關問題