如何调用我的自定义警报控制器功能以在其他视图控制器中显示?

时间:2015-06-15 04:12:07

标签: ios swift uialertcontroller

我已经编写了自定义警报视图控制器。但是,我在从其他视图控制器调用警报控制器时出错。它显示了我在下面描述的错误。

控制台上的错误

2015-06-15 10:21:50.610 automobile[4197:62165] Warning: Attempt to present <UIAlertController: 0x7d11cf70> on <automobile.LoadingAlertViewController: 0x7bfa55d0> whose view is not in the window hierarchy!

这是我的LoadingAlertViewController

import UIKit

class LoadingAlertViewController: UIAlertController {

var loadingAlertController : UIAlertController!

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

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

func displayLoadingAlert() -> UIAlertController {

    var controllerToPresent = viewController
    if controllerToPresent == nil {
            controllerToPresent = self
    }
    //create an alert controller
    loadingAlertController = UIAlertController(title: "Loading...", message: "We are receiving data from network.Please Wait.", preferredStyle: .Alert)

    let indicator = UIActivityIndicatorView()
    indicator.color = UIColor.redColor()
    indicator.setTranslatesAutoresizingMaskIntoConstraints(false)
    loadingAlertController.view.addSubview(indicator)

    let views = ["pending" : loadingAlertController.view, "indicator" : indicator]
    var constraints = NSLayoutConstraint.constraintsWithVisualFormat("V:[indicator]-(7)-|", options: nil, metrics: nil, views: views)
    constraints += NSLayoutConstraint.constraintsWithVisualFormat("H:|[indicator]|", options: nil, metrics: nil, views: views)
    loadingAlertController.view.addConstraints(constraints)

    indicator.userInteractionEnabled = false
    indicator.startAnimating()

    controllerToPresent!.presentViewController(loadingAlertController, animated: true, completion: nil)

    return loadingAlertController
}

func dismissLoadingAlert() -> UIAlertController {
    loadingAlertController.dismissViewControllerAnimated(true, completion: nil)
    return loadingAlertController
}

}

然后我在我的另一个视图控制器上解除了这个LoadingAlertViewController,我想在每次向API请求时显示它。

这是我的ViewController

import UIKit

class ViewController : UIViewControler{

var loadingAlertController : LoadingAlertController!
var api = MyAPI()

override func viewDidLoad() {

       loadingAlertController = LoadingAlertViewController()

       api.getResults()

       showAlert(true)

}

func showAlert(alert : Bool){

       if alert{

          loadingAlertController.displayLoadingAlert(self)

       }else{

          loadingAlertController.dismissLoadingAlert()

       }

}

任何帮助?请?如果有办法,如何从另一个视图控制器调用它。谢谢

2 个答案:

答案 0 :(得分:3)

像这样改变你的方法:

func displayLoadingAlert(viewController: UIViewController?) -> UIAlertController {
    var controllerToPresent = viewController
    if controllerToPresent == nil {
    controllerToPresent = self
}

// Most of your code

controllerToPresent.presentViewController(loadingAlertController, animated: true, completion: nil)

return loadingAlertController
}

然后当你拨打警报时:

loadingAlertController.displayLoadingAlert(self)

或者: 将方法displayLoadingAlert重命名为loadingAlert

删除该行:

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

然后在showAlert()方法内部调用

let loadingAlertController = loadingAlertController.loadingAlert()
self.presentViewController(loadingAlertController, animated: true, completion: nil)

答案 1 :(得分:2)

我看到你作为子类,但在UIAlertController文档中写道:

  

UIAlertController类旨在按原样使用,但不是   支持子类化。此类的视图层次结构是私有的   不得修改。

我建议你不要反对它。如果你需要一些非常自定义的东西来使用UIViewController并显示自定义行为。