Reject UIActionSheet programmatically in iOS 8, screw UIApplication windows / keyWindow
History:
I need to programmatically reject the UIActionSheet when the app goes into the background, and what I did in iOS 7 was to traverse [UIApplication sharedApplication].windows
and find a view that has a type UIActionSheet
and then reject it. It doesn't work in iOS 8.
Background:
In iOS 8, UIAlertController
should be used to represent warnings or action sheets, and a bit experiment shows that it is UIActionSheet
internally implemented with UIAlertController
:
po [[UIApplication sharedApplication] keyWindow] <_UIAlertControllerShimPresenterWindow:
po [[[UIApplication sharedApplication] keyWindow] rootViewController] <_UIAlertShimPresentingViewController: 0x7faa0cf049f0>
po [[[[UIApplication sharedApplication] keyWindow] rootViewController] presentedViewController] <UIAlertController: 0x7faa0c8cb9e0>
Who has the correct title, message, etc.
Confusion:
Then I added code to reject the controller and it rejects it. However, the application is now unresponsive to any user interaction and I found that after rejecting, keyWindow
still _UIAlertControllerShimPresenterWindow
, then I am confused, shouldn't reject the presented controller, clear its window? Or is it because it only rejects when the app goes into the background, so the window hierarchy is not in a normal state, so things get confusing?
Can anyone provide some opinions?
Thank!
source to share
[self.view.window makeKeyAndVisible];
Doesn't work completely. ShimPresenterWindow is still the main window.
So, I came up with another idea. Once you miss the alertController, paste in the following code:
NSString *windowDescription = NSStringFromClass([window class]);
if ([windowDescription isEqualToString:@"_UIAlertControllerShimPresenterWindow"]){
window.hidden = YES;
}
When combined with the creation of the main window, the visible file successfully removes the action sheet from the main view. Please note that I am not sure if this leads to memory leaks of any kind.
source to share