问题
On an iPhone running iOS 8, the code below causes an action sheet to pop up. However, on an iPad running iOS 8 the code below does not cause an action sheet to pop up and instead nothing happens.
NSUserDefaults *defauj = [NSUserDefaults standardUserDefaults];
NSArray *cod = [defauj objectForKey:@"customlistofstuff"];
UIActionSheet* actionSheet = [[UIActionSheet alloc] init];
actionSheet.delegate = self;
for(int i=0;i<[cod count];i++)
{
[actionSheet addButtonWithTitle:[cod objectAtIndex:i]];
}
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"None"];
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
回答1:
Try this:
[actionSheet showInView:[UIApplication sharedApplication].keyWindow.rootViewController.view];
It looks like you can't present action sheets on UIWindow
s directly anymore, you have to present them on an actual view that is managed by a view controller, so the root view controller's view is perfect for this.
I think this has less to do with the fact that UIActionSheet
is deprecated (and you can't just magically switch to UIAlertController
just yet if you need to support iOS 7), and more to do with the way their presentation is handled in the underlying implementation — I'm guessing it now relies on the view the sheet is presented in having a view controller, which is not true for windows.
EDIT: If you have a view controller presented modally over the top of the root view controller, this obviously won't work as the root view controller's view is no longer visible. You'll need to present the sheet in a view that is currently visible, e.g. the view of the current view controller (self.view
).
回答2:
I bet this has something to do with UIActionSheet
being deprecated in iOS 8. You're supposed to use UIAlertController instead with a preferredStyle
of UIAlertControllerStyleActionSheet
.
Try using that instead and see if it works. You'll have to use blocks instead of methods, but that shouldn't be too hard to do.
回答3:
According to Apple's Human Interface Guidelines about Temporary Views, A cancel button should only be used when the view presenting the action sheet is a popover, because, according to the HIG, users can tap outside the popover to dismiss the action sheet.
Therefore, cancel buttons do not work on iPads.
UIActionSheet
has also been deprecated, and you should use UIAlertController
instead.
来源:https://stackoverflow.com/questions/26170400/ios-8-ipad-only-bug-with-action-sheets