1

I am attempting to use a dedicated UIWindow for presenting UIAlertControllers. Everything was going pretty well until I realized that the handlers on the UIAlertActions attached to the UIAlertControllers were not being fired if they were presented by a controller on this other UIWindow.

If I present the UIAlertController from any viewController on the main window the handlers are fired, if I present it from the rootViewController of the alert window the handlers are not fired.

Any hints as to what I may be experiencing here? Thanks!

Edit: Test Project illustrating the problem.

Solved: I forgot to call the completion block in my override of dismissViewControllerAnimated:completion: in my alertWindowRootViewController subclass.

rzulkoski
  • 63
  • 1
  • 6
  • 1
    Does the alert controller behavior correctly otherwise? I mean, does it dismiss when a button is tapped? Update your question with relevant code. – rmaddy Oct 07 '15 at 22:37
  • @rmaddy It seems to be behaving normally otherwise. It does dismiss when buttons are tapped. Without the handlers it is rather useless though. Here's a [test project](https://github.com/RZulkoski/TestAlertWindow) I created illustrating the problem. – rzulkoski Oct 08 '15 at 13:34

2 Answers2

2

I can't imagine why you'd ever want to do this. But my guess is that Apple is optimizing away some actions on windows that aren't the key window.

Whatever it is the short answer is you shouldn't be doing this.

InkGolem
  • 2,662
  • 1
  • 15
  • 22
  • Actually according to [this answer](http://stackoverflow.com/a/30941356/1647916) this is how Apple actually implements showing their UIAlertControllers internally. Also this is a method that [Dylan Bettermann](http://stackoverflow.com/users/2088780/dylan-bettermann) has had [success with](http://stackoverflow.com/questions/28615147/uialertview-show-behavior-for-uialertcontroller). – rzulkoski Oct 08 '15 at 14:04
  • @rzulkoski if I'm not mistaken that method ended with UIAlertController. Previously with UIAlertView that was the only way to guarantee it would be on top, but with presentation controllers Apple was able to achieve more consistent behavior. – InkGolem Oct 08 '15 at 15:32
  • I don't think anyone can be sure outside of Apple, but the impression I got from the answer I linked in my comment above made me think otherwise. In the answer he states that at WWDC 2015 an Apple engineer told him that internally Apple is creating a `UIWindow` with a transparent `UIViewController` and then presenting the `UIAlertController` on it. Either way now that I have found and corrected my mistake it is working pretty well. /cheers – rzulkoski Oct 08 '15 at 19:37
2

So it turns out that I was forgetting to call the completion block in my override of dismissViewControllerAnimated:completion. Doh!

I have updated my Test Project to include the fix in hopes that this can be valuable to someone else.

I do like my solution more than Dylan Betterman's Solution since mine keeps a permanent window around that isn't associated with the lifecycle of the UIAlertController. This means that you are allowed to keep strong pointers to the alert controllers unlike Dylan's solution.

rzulkoski
  • 63
  • 1
  • 6