nicolaschengdev/WYPopoverController — pull request #54 by scastria (2013-11-28 08:28:58+00:00)
Title: Allow delegate to override key window

I was trying to present a WYPopoverController as a result of the user touching a menu item in an ActionSheet. The result was the WYPopoverController showed on the screen for about 0.5 seconds and then it disappeared. I put a break point in the dismiss code and it was never called. After further debugging, I realized that when an ActionSheet or a UIAlertView is on the screen, the default KeyWindow is NOT the real UIWindow of my application. Instead ActionSheet's and UIAlertView's create their own temporary UIWindow that is set as the KeyWindow as long as they are visible. Therefore, when my WYPopoverController was presented, its code that gets the SharedApplication KeyWindow got the temporary one from my ActionSheet just before it closed down. Therefore, when the ActionSheet finally closed down, its UIWindow goes away causing my WYPopoverController attached to it to go away as well.

The only fix I could think of was to make the WYPopoverController use the real UIWindow of my application even if a temporary UIWindow was on the screen at the time. So how does the WYPopoverController code know how to get a reference to the real UIWindow? I don't know. Therefore, I added a method to the delegate to allow the WYPopoverController to ask the delegate for a UIWindow to use when displaying itself. If the delegate does not implement this new method or returns null from this new method, WYPopoverController will just use the KeyWindow it has always been using.

I tried using this new delegate method in my application and it solved my problem. When the ActionSheet disappeared, it did NOT take the WYPopoverController down with it. It remained on the screen like I wanted.

If you can think of a better way to solve this issue, please implement that way instead. If not, here is a pull request with my change.