在Parse(SWIFT)上保存数据时打开弹出视图

时间:2017-07-06 20:23:04

标签: ios swift uiview popup

我在Swift上为应用程序创建了一个注册视图。我想在用户成功注册时加载弹出视图以添加更多数据。我正在使用Parse存储数据。

我正在使用此代码保存数据:

@IBAction func registerButtonPressed(_ sender: Any) {

    if emailTextField.text == "" || usuerTextField.text == "" || password1TextField.text == "" || password2TextField.text == "" {

        createAlert(title: "Error", message: "Fill all data")

    } else {

        if password1TextField.text != password2TextField.text {

            createAlert(title: "Error", message: "Passwords must be the same")
        } else {

            activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
            activityIndicator.center = self.view.center
            activityIndicator.hidesWhenStopped = true
            activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
            view.addSubview(activityIndicator)
            activityIndicator.startAnimating()
            UIApplication.shared.beginIgnoringInteractionEvents() // UIApplication.shared() is now UIApplication.shared

            let user = PFUser()

            user.username = usuerTextField.text
            user.email = emailTextField.text
            user.password = password1TextField.text

            let acl = PFACL()

            acl.getPublicWriteAccess = true

            user.acl = acl

            user.signUpInBackground(block: { (success, error) in

                self.activityIndicator.stopAnimating()
                UIApplication.shared.endIgnoringInteractionEvents() // UIApplication.shared() is now  UIApplication.shared

                if error != nil {

                    var displayErrorMessage = "Please try again later."

                    let error = error as NSError?

                    if let errorMessage = error?.userInfo["error"] as? String {

                        displayErrorMessage = errorMessage

                    }

                    self.createAlert(title: "Signup Error", message: displayErrorMessage)

                } else {


                }


            })
        }
    }

在else语句中,我想添加以下代码:

    let vc = (
        storyboard?.instantiateViewController(
            withIdentifier: "sbPopUpID")
        )!
    vc.modalTransitionStyle = .crossDissolve
    present(vc, animated: true, completion: nil)

但是只有在我在viewDidLoad和didReceiveMemoryWarning之后加载它才有效,¿如果没有保存用户数据的错误,我怎么能加载这个Pop Up?

谢谢

1 个答案:

答案 0 :(得分:0)

您的代码似乎没问题,您只是忘了在某些语句之前添加隐式self声明(默认情况下调试器会告诉您)。试试这个:

let vc = self.storyboard!.instantiateViewController(withIdentifier: "sbPopUpID")
vc.modalTransitionStyle = .crossDissolve
self.present(vc, animated: true)