iOS 8 Swift 1.2 和 Parse - 尝试在视图不在 window 层次结构中的 ViewController 上呈现 UIAlertController

iOS 8 Swift 1.2 and Parse - Attempt to present UIAlertController on ViewController whose view is not in the window hierarchy

Stack 上有很多关于此问题的问题,但其中 none 似乎解决了我遇到的问题。

我在应用程序的登录和注册部分使用 ParseUI。我希望希望发生的是当用户(例如)未在用户名和密码字段中输入任何文本时显示UIAlertController

这是我的 MasterViewController:

的代码
class MasterViewController: UIViewController, PFLogInViewControllerDelegate,
PFSignUpViewControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        if (PFUser.currentUser() == nil) {
            var logInViewController = LoginViewController()
            logInViewController.delegate = self

            var signUpViewController = SignUpViewController()
            signUpViewController.delegate = self

            logInViewController.signUpController = signUpViewController

            self.presentViewController(logInViewController, animated: true, completion: nil)

        }
    }

    func logInViewController(logInController: PFLogInViewController,
        shouldBeginLogInWithUsername username: String, password: String) -> Bool {

            if (!username.isEmpty || !password.isEmpty) {
                return true
            } else {
                let alertController = UIAlertController(title: "Failed to login.",
                    message: "Login Failure.  Please try again.", preferredStyle: .Alert)

                let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
                alertController.addAction(defaultAction)

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


                return false
            }
    }



    func logInViewController(logInController: PFLogInViewController,
        didFailToLogInWithError error: NSError?) {

            println("Failed to log in.")

    }


    func signUpViewController(signUpController: PFSignUpViewController,
        shouldBeginSignUp info: [NSObject : AnyObject]) -> Bool {

            if let password = info["password"] as? String {
                return count(password.utf16) >= 8
            } else {
                return false
            }
    }

    func signUpViewController(signUpController: PFSignUpViewController,
        didFailToSignUpWithError error: NSError?) {

            println("Failed to sign up.")

    }

    func logInViewController(logInController: PFLogInViewController,
        didLogInUser user: PFUser) {
            let installation = PFInstallation.currentInstallation()
            installation["user"] = PFUser.currentUser()
            installation.saveInBackground()
            self.dismissViewControllerAnimated(true, completion: nil)
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func signUpViewControllerDidCancelSignUp(signUpController:
        PFSignUpViewController) {

            println("User dismissed signup.")

    }
}

在阅读了另一个用户的答案后,我认为这就是答案,我将以下 class 添加到我的工作区:

import Foundation

class AlertHelper: NSObject {
    func showAlert(fromController controller: MasterViewController) {
        var alert = UIAlertController(title: "abc", message: "def", preferredStyle: .Alert)
        controller.presentViewController(alert, animated: true, completion: nil)
    }
}

然后相应地修改我的方法:

func logInViewController(logInController: PFLogInViewController, shouldBeginLogInWithUsername 用户名:String,密码:String) -> Bool {

    if (!username.isEmpty || !password.isEmpty) {
        return true
    } else {
        let alertController = UIAlertController(title: "Failed to login.",
            message: "Login Failure.  Please try again.", preferredStyle: .Alert)

        let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
        alertController.addAction(defaultAction)

        var alert = AlertHelper()
        alert.showAlert(fromController: self)

        return false
    }

}

此时我不太确定还能做什么。我的一个想法是以编程方式将 UILabel 添加到我的 LoginViewControllerSignUpViewController 并根据用户登录和注册的错误(或缺少错误)更改内容,但我会喜欢有提醒。

编辑:这是我 LoginViewController 中的代码。它 subclassed 是为了自定义外观。我的 SignUpViewController 的代码几乎相同。

导入 UIKit 导入解析 导入 ParseUI

class LoginViewController: PFLogInViewController {

  override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = ckPurple

    let label = UILabel()
    label.textColor = UIColor.whiteColor()
    label.text = "Welcome."
    label.sizeToFit()
    logInView?.logo = label

    // Do any additional setup after loading the view.
  }

问题是您从主视图控制器中呈现了一个登录视图控制器,这从视图层次结构中删除了主视图控制器。然后,您尝试从主视图控制器中呈现一个警报视图控制器,但您需要从视图层次结构中的视图控制器中呈现。尝试从您的登录视图控制器 显示警报

loginController.presentViewController(alertController, animated: true, completion: nil)