【问题标题】:UIViewController failing to navigate to another page in swiftUIViewController 无法快速导航到另一个页面
【发布时间】:2016-01-25 19:55:23
【问题描述】:

我以前写过这个函数,它工作正常。休息一周后,我再次开始从事该项目,但相同的代码无法正常工作。我在这里错过了什么吗?基本上,如果按下按钮,checkCredentials 就会运行。 “else”部分中的所有内容都运行良好,但 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)
    }
  }
}

【问题讨论】:

  • error 中有值吗?听起来 Parse 请求可能有问题
  • 您能否澄清一下您是否看到了 NSLog 消息?如果不是,则 user == nil 并且您的代码未执行或整个完成处理程序未执行(可能是由于响应错误)。事实上,后者会解释为什么它在一周前可以工作而现在不能工作:)

标签: ios swift uiviewcontroller segue viewcontroller


【解决方案1】:

这应该对你有用...

   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 客户端密钥(我以前做过),这将导致它不显示任何错误而只是无法工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    • 2015-03-18
    相关资源
    最近更新 更多