iOS 8 iPad only bug with action sheets

2019-04-15 07:32发布

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];

3条回答
劫难
2楼-- · 2019-04-15 07:54

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.

查看更多
Viruses.
3楼-- · 2019-04-15 07:57

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.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-04-15 08:08

Try this:

[actionSheet showInView:[UIApplication sharedApplication].keyWindow.rootViewController.view];

It looks like you can't present action sheets on UIWindows 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).

查看更多
登录 后发表回答