尝试呈现其视图不在窗口层次结构中的UIAlertController(Swift 3 / Xcode 8)

时间:2017-09-10 14:39:30

标签: ios swift uialertcontroller

我正在尝试创建应用,并且我想在出现登录错误时显示提醒,或者用户忘记输入用户名和/或密码。但是,我总是收到这个警告:

  

警告:尝试出现    谁的观点不在窗口   层次!

我已经尝试了我在这里找到的其他解决方案,但我仍然无法修复它。这是我的代码:

$items

更新:这是整个视图控制器

func createAlert(title: String, message: String) {

    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)

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

        self.dismiss(animated: true, completion: nil)

    }))

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

}

@IBAction func signInPressed(_ sender: Any) {

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

        createAlert(title: "Error in form", message: "Please enter an email and password.")

    } else {

        var activityIndicator = UIActivityIndicatorView()

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

        PFUser.logInWithUsername(inBackground: usernameTextField.text!, password: passwordTextField.text!, block: { (user, error) in

            activityIndicator.stopAnimating()
            UIApplication.shared.endIgnoringInteractionEvents()

            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: "Sign in error", message: displayErrorMessage)


            } else {

                print("Logged in")

                self.performSegue(withIdentifier: "toSignIn", sender: self)

            }


        })

    }

}

4 个答案:

答案 0 :(得分:2)

每当我们试图在任何闭包内部呈现UIAlertController时,都应在主线程上调用它,例如:

DispatchQueue.main.async { [weak self] in
    self?.createAlert(title: "Sign in error", message: displayErrorMessage)
}

答案 1 :(得分:1)

首先创建UIAlertController这样的属性。

var alertController: UIAlertController?

您必须在 viewDidLoad()中添加此内容,如下所示:

override func viewDidLoad() {
    super.viewDidLoad()

    self.alertController = UIAlertController(title: "Alert", message: "Not images yet", preferredStyle: .alert)
    self.alertController?.addAction(UIAlertAction(title: "Close", style: .default))
    view.addSubview((alertController?.view)!)

}

因此,当您按下signInButton并且登录不正确时,您必须调用。

@IBAction func signInPressed(_ sender: Any) {

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

    createAlert(title: "Error in form", message: "Please enter an email and password.")

} else {

    var activityIndicator = UIActivityIndicatorView()

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

    PFUser.logInWithUsername(inBackground: usernameTextField.text!, password: passwordTextField.text!, block: { (user, error) in

        activityIndicator.stopAnimating()
        UIApplication.shared.endIgnoringInteractionEvents()

        if error != nil {

            self.presentedViewController?.present(self.alertController!, animated: true, completion: nil)
        }
}

答案 2 :(得分:0)

将您的createAlert方法更改为此

self.createAlert(title: "Sign in error", message: "Please try again later.", controller: self)

然后创建这样的提醒,

{{1}}

这可能会解决您的问题。

答案 3 :(得分:0)

尝试 Swift 3

的此代码
func displayMyAlertMessageError(userMessage:String, titleHead: String)
    //define displyMyAlertMessage.
{
    let MyAlert = UIAlertController(title: titleHead, message: userMessage, preferredStyle:UIAlertControllerStyle.alert);
    let okAction = UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil);MyAlert.addAction(okAction);
    self.present(MyAlert,animated:true,completion:nil);
}

@IBAction func signInPressed(_ sender: Any) {

  if (usernameTextField.text?.isEmpty) || (passwordTextField.text?.isEmpty)
    {
      createAlert(title: "Error in form", message: "Please enter an email and password.")
    }
  else 
    {
      //Your code here
    }