3

I have got a UIAlertController that I would like to present within a UITableViewController. However when I run the code I get the following error:

Warning: Attempt to present on whose view is not in the window hierarchy!

My code seem to reflect what suggested in this answer. What am I doing wrong?

override func viewDidLoad() {
    super.viewDidLoad()

    // Check for force touch feature, and add force touch/previewing capability.
    if traitCollection.forceTouchCapability == .available {
        registerForPreviewing(with: self, sourceView: view)
    }
    else {
        // Create an alert to display to the user.
        alertController = UIAlertController(title: "3D Touch Not Available", message: "Unsupported device.", preferredStyle: .alert)
        self.present(alertController!, animated: false, completion: nil)
    }
Community
  • 1
  • 1
mm24
  • 9,280
  • 12
  • 75
  • 170
  • Possible duplicate of [whose view is not in the window hierarchy](http://stackoverflow.com/questions/11862883/whose-view-is-not-in-the-window-hierarchy) – Adarkas2302 Jan 19 '17 at 09:44

1 Answers1

3

Suppose, in viewDidLoad you still don't have a view in the window hierarchy, like said in message. Try to move your code to viewDidAppear instead

Vladyslav Zavalykhatko
  • 15,202
  • 8
  • 65
  • 100