if或/和if if语句被传递

时间:2016-01-26 00:24:48

标签: ios swift if-statement

我正在为我的应用程序构建一个自定义解析登录,但我无法在loginBTN Action中获取我的一个if语句来执行。一旦我使用loginSwitch.on == true登录,即使我的用户名和密码文本字段为空/零,它也会转到配置文件视图,即打开最后一次登录用户的应用程序。在我的代码中,我试图让它显示一个警告,要求输入用户名和密码。更令人困惑的是,当调用else if loginSwitch.on == false并且3个文本字段为空时,我收到了警报消息。请帮助在“登录”状态/ loginSwitch.on == true中停止自动segue。

 @IBAction func loginBTN(sender: AnyObject)
    {
        if loginSwitch.on == true
        {
            if usernameTXTFLD != nil || passwordTXTFLD != nil
            {
                PFUser.logInWithUsernameInBackground(usernameTXTFLD.text!, password:passwordTXTFLD.text!) {
                    (user: PFUser?, error: NSError?) -> Void in
                    if user == user
                    {
                        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                            self.performSegueWithIdentifier("loginToProfileSegue", sender: self)
                        })
                    } else {
                        let errorString = error!.userInfo["error"] as? String
                        self.displayAlert("Log In Failed", message: errorString!)
                    }
                }
            }
            else
            {
                self.displayAlert("Log In Failed", message: "Error: Please Provide Unique Username and Password")
            }
        }
        else
        {
            if self.usernameTXTFLD != nil || self.passwordTXTFLD != nil || self.emailTXTFLD != nil
            {
                let user = PFUser()
                user.username = usernameTXTFLD.text
                user.password = passwordTXTFLD.text
                user.email = emailTXTFLD.text
                user.signUpInBackgroundWithBlock {
                    (succeeded: Bool, error: NSError?) -> Void in
                    if let _ = error
                    {
                        self.displayAlert("Log In Failed", message: "Error: Please Provide Unique Username, Password, and Email Address")
                    }
                    else
                    {
                        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                            self.performSegueWithIdentifier("loginToProfileSegue", sender: self)
                        })
                    }
                }

            }
            else if usernameTXTFLD == nil || passwordTXTFLD == nil || emailTXTFLD == nil
            {
                //Alert - Please provide username, password, and emailaddress
                self.displayAlert("Signup Failed", message: "Error: Please Provide Unique Username, Password, and Email Address")
            }
        }

    }

1 个答案:

答案 0 :(得分:2)

你有一些问题。您的一些nil项检查不正确,您正在检查文本字段本身是否为nil,而不是检查文本字段中是否有任何文字。

if loginSwitch.on == true
{
    if usernameTXTFLD.text!.isEmpty == false && passwordTXTFLD.text!.isEmpty == false
    {
        PFUser.logInWithUsernameInBackground(usernameTXTFLD.text!, password:passwordTXTFLD.text!) {
            (user: PFUser?, error: NSError?) -> Void in
            if user == user
            {
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    self.performSegueWithIdentifier("loginToProfileSegue", sender: self)
                })
            } else {
                let errorString = error!.userInfo["error"] as? String
                self.displayAlert("Log In Failed", message: errorString!)
            }
        }
    }
    else
    {
        self.displayAlert("Log In Failed", message: "Error: Please Provide Unique Username and Password")
    }
}
else
{
    if self.usernameTXTFLD.text!.isEmpty == false && self.passwordTXTFLD.text!.isEmpty == false && self.emailTXTFLD.text!.isEmpty == false
    {
        let user = PFUser()
        user.username = usernameTXTFLD.text
        user.password = passwordTXTFLD.text
        user.email = emailTXTFLD.text
        user.signUpInBackgroundWithBlock {
            (succeeded: Bool, error: NSError?) -> Void in
            if let _ = error
            {
                self.displayAlert("Log In Failed", message: "Error: Please Provide Unique Username, Password, and Email Address")
            }
            else
            {
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    self.performSegueWithIdentifier("loginToProfileSegue", sender: self)
                })
            }
        }

    }
    else
    {
        //Alert - Please provide username, password, and emailaddress
        self.displayAlert("Signup Failed", message: "Error: Please Provide Unique Username, Password, and Email Address")
    }
}