代码之家  ›  专栏  ›  技术社区  ›  acoustickat

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

  •  1
  • acoustickat  · 技术社区  · 7 年前

    警告:试图在上显示 等级制度

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

    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)
    
                }
    
    
            })
    
        }
    
    }
    

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

    class ViewController: UIViewController {
    
    @IBOutlet var usernameTextField: UITextField!
    @IBOutlet var passwordTextField: UITextField!
    
    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)
    
                }
    
    
            })
    
        }
    
    }
    
    override func viewDidAppear(_ animated: Bool) {
    
    
        if PFUser.current() != nil {
    
            performSegue(withIdentifier: "toSignIn", sender: self)
    
        }
    
        self.tabBarController?.tabBar.isHidden = true
    
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
    }
    
    4 回复  |  直到 7 年前
        1
  •  2
  •   Miguel Ángel Jareño Escudero    7 年前

    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)!)
    
    }
    

    @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
  •  2
  •   Nitesh    4 年前

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

    DispatchQueue.main.async { [weak self] in
        self?.createAlert(title: "Sign in error", message: displayErrorMessage)
    }
    
        3
  •  0
  •   Wide Angle Technology    7 年前

    尝试此代码 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
        }
    
        4
  •  -1
  •   Bishan    7 年前

    将createAlert方法更改为,

    func createAlert(title: String, message: String, controller: UIViewController) {
    
            let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
    
                self.dismiss(animated: true, completion: nil)
    
            }))
            controller.present(alert, animated: true, completion: nil)
    
        }
    

    然后创建这样的警报,

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

    这可能会解决你的问题。