即使满足条件(值为空)也不会显示警报控制器

时间:2018-12-13 18:56:46

标签: ios swift

我的个人应用程序一直存在问题,某些用户在注册时似乎丢失了个人资料照片(字符串)。我添加了一项检查以确保当用户按下“下一步”按钮时字符串不为空-基本检查字符串是否为"",如果是,请显示一个警报控制器以提醒他们选择个人资料图片。如果不为空,则选择下一个屏幕。这是相关的代码(为清楚起见,var emailUserPicString = ""是全局变量):

   emailUserPicString = url.absoluteString
    print("\n\n\npic:\(emailUserPicString)\n\n\n")

    if emailUserPicString == "" {
        let alertController = UIAlertController(title: "Profile Picture Error", message: "Don't forget to choose a profile picture!", preferredStyle: UIAlertControllerStyle.alert)

        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {(alert :UIAlertAction!) in
        })
        alertController.addAction(okAction)

        self.present(alertController, animated: true, completion: nil)

        return
    } else {
        self.performSegue(withIdentifier: "emailToSetup", sender: nil)
    }

现在,当我测试此解决方案时,按Next按钮不会执行任何操作,并且不会显示警报。我认为这将是一个或另一个-如果字符串为空,将显示警报控制器,如果有值,将发生performSegue,我们将转到下一个屏幕。我想知道为什么这些都不发生

如果可以提供某些上下文,则这是完整的功能:

@IBAction func emailSignupNextPressed(_ sender: Any) {
    // Make sure text fields aren't empty
    guard nameField.text != "", emailField.text != "", passwordField.text != "", confirmPasswordField.text != "" else {return}

    if passwordField.text == confirmPasswordField.text {
        Auth.auth().createUser(withEmail: emailField.text!, password: passwordField.text!, completion: { (user, error) in

            if let error = error {
                print(error.localizedDescription)
            }

            if let user = user {

                guard let uid = Auth.auth().currentUser?.uid else {return}

                // Use name as Firebase display name for readability
                let changeRequest = Auth.auth().currentUser!.createProfileChangeRequest()
                changeRequest.displayName = self.nameField.text!
                changeRequest.commitChanges(completion: nil)

                // Create child node from userStorage "users". Profile image set to user's unique ID
                let imageRef = self.userStorage.child("\(uid).jpg")
                let data = UIImageJPEGRepresentation(self.selectProfileImageView.image!, 0.5)

                // Upload image to Firebase
                let uploadTask = imageRef.putData(data!, metadata: nil, completion: { (metadata, err) in
                    if err != nil {
                        print(err!.localizedDescription)
                    }
                    imageRef.downloadURL(completion: { (url, er) in
                        if er != nil {
                            print(er?.localizedDescription as Any)
                        }
                        if let url = url {

                            emailUserPicString = url.absoluteString
                            print("\n\n\npic:\(emailUserPicString)\n\n\n")

                            if emailUserPicString == "" {
                                let alertController = UIAlertController(title: "Profile Picture Error", message: "Don't forget to choose a profile picture!", preferredStyle: UIAlertControllerStyle.alert)

                                let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {(alert :UIAlertAction!) in
                                })
                                alertController.addAction(okAction)

                                self.present(alertController, animated: true, completion: nil)

                                return
                            } else {
                                self.performSegue(withIdentifier: "emailToSetup", sender: nil)
                            }
                        }
                    })
                })
                uploadTask.resume()
            }
        })
    } else {
        //            print("Passwords don't match")
        passwordAlert()
    }
}

我的问题是,我是否正确处理了字符串检查?如果那里没有价值,为什么我的警报控制器不显示?如果有一个值,为什么不执行segue?

3 个答案:

答案 0 :(得分:1)

如我所见,您正在后台线程中显示UIAlertAction。因此,您应该使用以下方法在UI线程中显示它:

DispatchQueue.main.async {
  // show alert here
}

答案 1 :(得分:0)

尝试将if语句更改为

if emailUserPicString.isEmpty || emailUserPicString.isEmpty == ""{

}

也许空的textField没有== ""字符串

答案 2 :(得分:-1)

遵循此解决方案:

private func validateForm() {

    if emailTextField.text?.isEmpty {
        // Show alert message like: Please enter the email addess.
        return
    }

    if passwordTextField.text?.isEmpty && passwordTextField.text?.count < 6 {
        // Show alert message like: Password must be at least 6 chars long.
        return
    }

    if profleImage.image == nil {
        // Show alert message like: Please choose your profile photo.
        return
    }

    // Now you can post API request after validating all values.
    // Call here API request method.....
}