¿Cómo llamar a mi función de controlador de alerta personalizada para que se muestre en otro controlador de vista?
He escrito mi controlador de vista de alerta personalizado. Pero tengo un error al llamar a mi controlador de alerta desde otro controlador de vista. Me muestra el error que describí a continuación.
Error en la consola
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!
Aquí está miLoadingAlertViewController
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
}
}
Entonces decalore estoLoadingAlertViewController
en mi otro controlador de vista donde quiero mostrar cada vez que solicito a mi API.
Este es miViewController
.
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()
}
}
¿Alguna ayuda? Por favor, si hay una manera, cómo llamarla desde otro controlador de vista. Gracias