Swift错误警报操作执行Segue,但成功的操作不会

时间:2015-04-09 01:52:29

标签: ios swift

我遇到了我的注册控制器的问题,当用户将任何文本字段留空时,我的错误处理程序会触发一次警告:#34; OK"单击将用户发送到下一个视图控制器,该控制器仅在注册成功时才会出现。当用户成功注册时,会发生相反的情况。我的信息被记录下来,但下一个视图控制器没有发生任何信号。我应该如何防止这种情况发生?

import UIKit

class UserRegistrationViewController: UIViewController {

    @IBOutlet var usernameTextField: UITextField!

    @IBOutlet var emailTextField: UITextField!

    @IBOutlet var passwordTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func registerUser(sender: AnyObject) {

        var error = ""

        if usernameTextField.text == "" || emailTextField.text == "" || passwordTextField.text == "" {

            error = "Please enter a username, email and password"

        }


        if error != "" {

            var alert = UIAlertController(title: "Error In Form", message: error, preferredStyle: UIAlertControllerStyle.Alert)

            alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: {
                action in

                self.dismissViewControllerAnimated(true, completion: nil)


            }))

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


        } else {

            var user = PFUser.currentUser()

            user.username = usernameTextField.text
            user.password = passwordTextField.text
            user.email = emailTextField.text

            user.saveInBackgroundWithBlock {
                (succeeded: Bool!, signupError: NSError!) -> Void in
                if signupError == nil {

                    println(user.username)
                    println(user.password)
                    println(user.email)




                    // Hooray! Let them use the app now.
                } else {
                    //let errorString = signupError.userInfo["error"] as NSString
                    // Show the errorString somewhere and let the user try again.

                    println("Error Registering")

                }
            }


        }


    }



    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

1 个答案:

答案 0 :(得分:0)

我很确定如果textfield为空,则返回nil而不是空字符串。因此,只需检查nil而不是空字符串就可以解决您的问题。这种变化发生在iOS 7上。为了确保你可以检查nil和amp; “”。希望这有帮助

修改

如果你不想,你可以这样做:

if (usernameTextField.text == nil || usernameTextField.text.length > n) || ...

其中n是您指定的数字。 这样,您可以确保用户不会使用太短的用户名或密码进行注册。当然,这取决于您的要求。

<强>更新

创建user对象时,请将其设置为PFUser.currentUser() which will be nil, because no one is logged in

所以你需要做的就是声明var user = PFUser()它应该有效。 Reference

相关问题