0

I guess the answer is easy but I've been days trying to solve the problem and can not find the solution. This is a UIViewController after certain checks and without using any button, display a UIAlertController. Always appear Warning: Attempt to present on whose view is not in the window hierarchy!

import UIKit

class ViewController: UIViewController {

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

func noData(){
    let alertController = UIAlertController(title: "Message", message: "There area no data in the file" , preferredStyle: .Alert)
    let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
    alertController.addAction(defaultAction)
    self.presentViewController(alertController, animated: true, completion: nil)
}

}

1 Answers1

0

You just need to call your method noData() inside viewDidAppear instead of viewDidLoad.

override func viewDidAppear(animated: Bool) {
    noData()
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • Many thanks. Yes in this case "works" but I've used the same procedure in another more complex viewController and the system shows again: Warning: Attempt to present on whose view is not in the window hierarchy!. Any idea?. Thanks in advance. – Capita-tic Oct 08 '15 at 02:27