AlertController不在窗口层次结构中 [英] AlertController is not in the window hierarchy

查看:82
本文介绍了AlertController不在窗口层次结构中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚使用ViewController类创建了一个单视图应用程序项目。我想从一个位于我自己的类中的函数中显示一个UIAlertController。

I've just created a Single View Application project with ViewController class. I would like to show a UIAlertController from a function which is located inside my own class.

这是我的班级警报。

class AlertController: UIViewController {
     func showAlert() { 
         var alert = UIAlertController(title: "abc", message: "def", preferredStyle: .Alert)
         self.presentViewController(alert, animated: true, completion: nil)
     }
}

这是ViewController执行的警告。

Here is ViewController which executes the alert.

class ViewController: UIViewController {
   override func viewDidLoad() {
       super.viewDidLoad()  
   }

   @IBAction func showAlertButton(sender: AnyObject) {
       var alert = AlertController()
       alert.showAlert()
   }
}

这是我得到的而不是美丽的警报。

This is what I get instead of beautiful alert.

警告:尝试在Sprint1.AlertController上显示UIAlertController:0x797d2d20:0x797cc500,其视图不在窗口层次结构中!

Warning: Attempt to present UIAlertController: 0x797d2d20 on Sprint1.AlertController: 0x797cc500 whose view is not in the window hierarchy!

我该怎么办?

推荐答案

让我们来看看你的观点层次结构。你有一个 ViewController
然后你创建了一个 AlertController ,你没有将它添加到你的层次结构中而你正在调用一个实例方法,它试图使用 AlertController 作为呈现控制器,只显示另一个控制器( UIAlertController )。

Let's look at your view hierarchy. You have a ViewController. Then you are creating an AlertController, you are not adding it to your hierarchy and you are calling an instance method on it, that attempts to use the AlertController as presenting controller to show just another controller (UIAlertController).

+ ViewController
    + AlertController (not in hierarchy)
        + UIAlertController (cannot be presented from AlertController)

简化代码

class ViewController: UIViewController {
   override func viewDidLoad() {
       super.viewDidLoad()  
   }

   @IBAction func showAlertButton(sender: AnyObject) {
       var alert = UIAlertController(title: "abc", message: "def", preferredStyle: .Alert)
       self.presentViewController(alert, animated: true, completion: nil)
   }
}

这将有效。

如果您需要某些东西的 AlertController ,你必须先将它添加到层次结构中,例如使用 addChildViewController 或使用另一个 presentViewController 调用。

If you need the AlertController for something, you will have to add it to the hierarchy first, e.g. using addChildViewController or using another presentViewController call.

如果你希望这个类只是一个创建警报的助手,它应该是这样的:

If you want the class to be just a helper for creating alert, it should look like this:

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

称为

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

这篇关于AlertController不在窗口层次结构中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆