1

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];
Hamlier Walp
  • 275
  • 4
  • 11

3 Answers3

6

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).

Greg
  • 9,068
  • 6
  • 49
  • 91
  • I use this code myself throughout my app, and it works for me. Try using your application delegate's window instead: `[[[UIApplication sharedApplication] delegate] window].rootViewController.view` – Greg Oct 04 '14 at 00:11
  • When I do that, it still doesn't work but at least I get an error in the debugger: Warning: Attempt to present on whose view is not in the window hierarchy – Hamlier Walp Oct 04 '14 at 01:02
  • @HamlierWalp do you have a view controller presented modally over the root view controller? If you do, try presenting the action sheet in `self.view`. – Greg Oct 04 '14 at 01:04
0

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.

Undo
  • 25,519
  • 37
  • 106
  • 129
  • 1
    It may be inconvenient to switch to using `UIAlertController` if you still have to support iOS 7, as this will result in duplicated code. I have a different, and I suspect more plausible, explanation for this issue (see my answer). – Greg Oct 03 '14 at 00:47
0

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.

ricky3350
  • 1,710
  • 3
  • 20
  • 35