iOS – UIActionsheet IPAD crashes from the child view controller

I apologize if anyone asked this, but I can’t seem to find it anywhere. I even recreated my question in a demo project in case you guys want to see it for yourself To it, although I don’t know where I should post it.

I have an application based on xibless UINavigationController. Some of my child ViewControllers have a button on the right side of the top and then display one UIActionSheet. My app is designed for iPhone and iPad, so when I am ready to display UIActionSheet, I will:

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle: [NSString stringWithFormat:@"%@ Menu", [self title]] delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Email", @"Print", nil];
[actionSheet setActionSheetStyle :UIActionSheetStyleDefault];
if ([actionSheet respondsToSelector:@selector(showFromBarButtonItem:animated:)])
[actionSheet showFromBarButtonItem:[[self navigationItem] rightBarButtonItem] animated:YES];
else [ actionSheet showInView:[self view]];
[actionSheet release];

On the iPad, I tried to display the UIActionSheet on the right bar button, on the iPhone it should slide from the bottom Enter. All of this is wonderful.

Unfortunately, if you click the button and display the menu on the iPad, and then click the back button at the top left of the app, the menu will not be ignored. On the contrary, UINavigationController due diligence Responsibly rebound, UIActionSheet still exists. If you try to click on something on the menu, you will of course crash. If the user clicks on anything else on the screen instead of the “back” button, the menu will be dismissed correctly.

If you try this test on your iPhone, everything works as expected. No problem.

My demo project has an AppDelegate and a ViewController that’s it. AppDelegate builds an NSDictionary of NSDictionaries, so I have a model through which I can demonstrate this problem. ViewController displays all the keys of the dictionary, if the corresponding value is NSDictionary, you can click on it to drill down.

This is an interesting question. This is what UIActionSheet Class Reference says.

On iPad, this method presents the action sheet in a popover and adds
the toolbar that owns the button to the popover’s list of passthrough
views. Thus, taps in the toolbar result in the action methods of the
corresponding toolbar items being called. If you want the popover to
be dismissed when a different toolbar item is tapped, you must
implement that behavior in your action handler methods.

So when you display the action sheet, it will automatically create a UIPopoverController and set the included toolbar (or navigation bar) as the passthrough view of the popover, thus allowing the touch event to continue. I think the best way is to Create an instance variable on your worksheet and force Control it to show whether it is visible in -viewWillDisappear:

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated] ;

if (self.actionSheet.window) // If action sheet is on screen, window is non-nil
[self.actionSheet dismissWithClickedButtonIndex:self.actionSheet.cancelButtonIndex animated:animated] ;
}

I apologize if anyone asked about this, but I can’t seem to find it anywhere. I even recreated me in a demo project The question, in case you guys want to see it in person, although I don’t know where I should post it.

I have an application based on xibless UINavigationController. Some of my child ViewControllers are at the top There is a button on the right side and a UIActionSheet is displayed. My app is designed for iPhone and iPad, so when I am ready to display UIActionSheet, I will:

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:[NSString stringWithFormat:@"%@ Menu", [self title]] delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Email", @"Print", nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleDefault];
if ([actionSheet respondsToSelector:@selector(showFromBarButtonItem:a nimated:)])
[actionSheet showFromBarButtonItem:[[self navigationItem] rightBarButtonItem] animated:YES];
else [actionSheet showInView:[self view]];
[actionSheet release];< /pre>

On the iPad, I tried to display the UIActionSheet on the right bar button, and on the iPhone it should slide in from the bottom. All of this is wonderful.

Unfortunately , If you click the button and display the menu on the iPad, and then click the back button at the top left of the app, the menu will not be ignored. On the contrary, the UINavigationController bounces back dutifully, and the UIActionSheet still exists. If you try to click something on the menu , Of course you will crash. If the user clicks on anything else on the screen instead of the "back" button, the menu will be dismissed correctly.

If you try this test on an iPhone, everything is as expected It works. No problem.

My demo project has an AppDelegate and a ViewController that's it. AppDelegate builds an NSDictionary of NSDictionaries, so I have a model through which I can demonstrate this problem. ViewController displays the dictionary If the corresponding value is NSDictionary, you can click on it to drill down.

This is an interesting question. This is UIActionSheet Class Reference says.

On iPad, this method presents the action sheet in a popover and adds
the toolbar that owns the button to the popover's list of passthrough
views. Thus, taps in the toolbar result in the action methods of the
corresponding toolbar items being called. If you want the popover to
be dismissed when a different toolbar item is tapped, you must
implement that behavior in your action handler methods.

When table, it will automatically create UIPopoverController and set the contained toolbar (or navigation bar) as the passthrough view of the popover, thus allowing touch events to continue. I think the best way is to create an instance variable for your worksheet, and Force it to show whether it is visible in -viewWillDisappear:

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated] ;

if (self.actionSheet.window) // If action sheet is on screen, window is non-nil
[self.actionSheet dismissWithClickedButtonIndex:self.actionSheet.cancelButtonIndex animated:animated] ;
}

Leave a Comment

Your email address will not be published.