简体   繁体   English

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

[英]Swift Error Alert Action Performs Segue, But Successful Action Does Not

I am running into an issue with my signup controller where my error handler when a user leaves any text field blank fires an alert that once "OK" is clicked send the user to the next view controller that should appear only if the registration is successful. 我遇到了我的注册控制器的问题,当用户将任何文本字段留空时,我的错误处理程序都会发出警报,提示单击“确定”后,会将用户发送到仅在注册成功后才出现的下一个视图控制器。 The opposite is occurring when a user successfully registers. 当用户成功注册时,情况正好相反。 My information is recorded, but no segue happens to the next view controller. 我的信息已记录,但下一个视图控制器未发生中断。 How should I prevent this from happening? 我应该如何防止这种情况发生?

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.
    }
    */

}

I'm pretty sure that if a textfield is empty, it returns nil as opposed to and empty string. 我很确定,如果文本字段为空,则返回nil ,而不是空字符串。 So simply checking for nil instead of an empty string should fix your issue. 因此,只需检查nil而不是空字符串即可解决您的问题。 The change happened with iOS 7. Just to be sure you could check for both nil & "". 更改发生在iOS 7中。请确保可以同时检查nil和“”。 Hope this helps 希望这可以帮助

EDIT 编辑

If you wan't to you could do this: 如果您不愿意,可以这样做:

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

Where n is a number you specify. 其中n是您指定的数字。 That way you can make sure that a user wouldn't sign up with a username or password that is too short. 这样,您可以确保用户不会使用过短的用户名或密码进行注册。 Depending on your requirements of course. 当然取决于您的要求。

UPDATE: 更新:

When you create your user object you set it to PFUser.currentUser() which will be nil, because no one is logged in . 创建user对象时,请将其设置为PFUser.currentUser() ,因为没有人登录,它将为nil

So all you need to do is to declare var user = PFUser() and it should work. 因此,您所需要做的就是声明var user = PFUser() ,它应该可以工作。 Reference 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM