Thiha Aung
Thiha Aung

Reputation: 5106

How to call my custom alert controller function to display in other view controller?

I have write my custom alert view controller.But,I am having error at calling my alert controller from other view controller.It show me the error that I described below.

Error on console

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!

Here is my 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
}

}

Then I decalare this LoadingAlertViewController at my another view controller where i want to display every time that i request to my API.

This is my 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()

       }

}

Any Help?Please?If there is a way,how to call it from another view controller.Thank you

Upvotes: 3

Views: 1573

Answers (2)

Ramis
Ramis

Reputation: 16609

I see you made as subclass, but in the UIAlertController documentation is written:

The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

I would recommend you do not go against it. If you need something very custom better to use UIViewController and display with custom behavior.

Upvotes: 2

David Wong
David Wong

Reputation: 10294

Change your method like so:

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
}

Then when you're calling the alert:

loadingAlertController.displayLoadingAlert(self)

Alternatively: Rename the method displayLoadingAlert to loadingAlert

Remove the line:

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

then when calling insidethe showAlert() method

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

Upvotes: 3

Related Questions