【问题标题】:UIAlertController whose view is not in the window hierarchy视图不在窗口层次结构中的 UIAlertController
【发布时间】:2015-10-02 07:08:52
【问题描述】:

我正在尝试使用 Swift 和 Parse 创建一个应用程序。我正在使用 Xcode 7 和 Swift 2。 我想在用户登录失败时显示警报消息,这是我的功能:

func logInViewController(logInController: PFLogInViewController, didFailToLogInWithError error: NSError?){
    let alertLoginFailed = UIAlertController(title: "Login Failed", message: "Your username or password is invalid!", preferredStyle: UIAlertControllerStyle.Alert)
    alertLoginFailed.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alertLoginFailed, animated: true, completion: nil)
    print("Login failed.........!")
}

但我在模拟器中运行时出现此错误:

2015-10-02 11:32:39.988 RedString[2089:886501] Warning: Attempt to present <UIAlertController: 0x7a934400> on <MyProject.ViewController: 0x7b985150> whose view is not in the window hierarchy!
Login failed.........!

我用谷歌搜索过,但没有找到明确的解决方案。

这是我的全班:

import UIKit
import Parse
import ParseUI
class ViewController: UIViewController, PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(animated: Bool) {
        self.setupLoginView()
    }

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

    func logInViewController(logInController: PFLogInViewController, didLogInUser user: PFUser){
        self.dismissViewControllerAnimated(true, completion: nil)
    }

    func logInViewController(logInController: PFLogInViewController, didFailToLogInWithError error: NSError?){
        let alertLoginFailed = UIAlertController(title: "Login Failed", message: "Your username or password is invalid!", preferredStyle: UIAlertControllerStyle.Alert)
        alertLoginFailed.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alertLoginFailed, animated: true, completion: nil)
        print("Login failed.........!")
    }

    func signUpViewController(signUpController: PFSignUpViewController, didSignUpUser user: PFUser){
        self.dismissViewControllerAnimated(true, completion: nil)
    }

    func signUpViewController(signUpController: PFSignUpViewController, didFailToSignUpWithError error: NSError?){
        print("Sign up failed.........!")
    }

    func setupLoginView(){
        if(PFUser.currentUser() == nil){
            let loginViewController = PFLogInViewController()
            loginViewController.delegate = self
            let signUpViewController = PFSignUpViewController()
            signUpViewController.delegate = self
            loginViewController.logInView!.logo = UIImageView(image: UIImage(named:"logo.png"))

            loginViewController.signUpController = signUpViewController
            self.presentViewController(loginViewController, animated: true, completion: nil)
        }else{
            print("login as: " + PFUser.currentUser()!.username!)
            //prepare new view here
        }

    }
}

【问题讨论】:

  • 可能是因为调用 loginViewController 委托 didFailToLogInWithError 时您的 viewController 不在屏幕上?
  • 尝试用 logInContorller.presentViewController 代替 self.presentViewController

标签: swift swift2 uialertcontroller


【解决方案1】:

func logInViewController(logInController: PFLogInViewController, didFailToLogInWithError error: NSError?){
        let alertLoginFailed = UIAlertController(title: "Login Failed", message: "Your username or password is invalid!", preferredStyle: UIAlertControllerStyle.Alert)
        alertLoginFailed.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alertLoginFailed, animated: true, completion: nil)
        print("Login failed.........!")
    }

被调用,您的视图控制器不在屏幕上。因此,您可以

  • 关闭 logInViewController,然后显示警报并通过 setUpLogInView 作为处理程序。

  • 或者在上面的函数中,试试logInController.presentViewController 而不是self.presentViewController

【讨论】:

    【解决方案2】:

    你必须在你的登录视图控制器上调用presentViewController:animated:completion

    这是您的课程的简化版本,向您展示我的意思:

    class ViewController: UIViewController {
        weak var loginViewController: UIViewController?
    
        func setupLoginViewController() {
            let loginVC = PFLogInViewController()
            // setup loginVC
            loginViewController = loginVC // store the reference
        }
    
        func loginDidFail() {
            let alertVC = UIAlertController(...)
            // setup alertVC
            loginViewController?.presentViewController(...) // present the alert from the login view controller
        }
    }
    

    【讨论】:

    • 没问题!请将其中一个答案标记为“已接受”,因此请将此问题标记为已回答。
    【解决方案3】:

    我在 Objective-C 中遇到了同样的问题,它发生在父窗口尚未绘制到屏幕上时。我通过从 viewDidAppear 而不是 viewDidLoad 中调用代码解决了这个问题。见UIAlert error, whose view is not in the window hierarchy

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-06
      • 2023-03-31
      • 2017-07-27
      • 1970-01-01
      • 2016-01-06
      • 1970-01-01
      • 2020-08-07
      • 1970-01-01
      相关资源
      最近更新 更多