Disconnect UIActionSheet from UIBarButtonItem on iPad

I have a UIToolbar with several UIBarButtonItem that show different UIActionSheet using showFromBarButtonItem:

On an iPad, when one of these action sheets is on the screen, touching anywhere outside the Action sheet deletes it and does nothing (for example, touching a button does not call a button). This by design is something that I don't like, but I accept it for now, as long as it's normal behavior.

There is one exception. If I touch another UIBarButtonItem , this button starts and the current action screen is NOT removed from the screen. If a new button launches another UIActionSheet , I get two (or more) action sheets on the screen.

Of course, I can go through the tedious process of remembering what is on the screen and manually discard it, but I'm also concerned about the user, as some touches (those that focus on the toolbar buttons) are respected, while others are ignored.

Is there anything I can do, or do I need to live with this situation?

+6
source share
1 answer

This seems like an annoying inconsistency, but not something to fix the situation. This example assumes that you only need to show UIActionSheets , which seems random to you.

Here is an example of how to fix this starting with .h :

 @interface TestToolBarExclusiveActionSheet : UIViewController <UIActionSheetDelegate>{ UIActionSheet *sheetShowing; } @property (weak, nonatomic) IBOutlet UIBarButtonItem *oneButton; @property (weak, nonatomic) IBOutlet UIBarButtonItem *twoButton; @end 

and .m :

 #import "TestToolBarExclusiveActionSheet.h" @implementation TestToolBarExclusiveActionSheet @synthesize oneButton; @synthesize twoButton; -(IBAction)targetForBothButtons:(UIBarButtonItem *)button{ if (sheetShowing != nil){ [sheetShowing dismissWithClickedButtonIndex:[sheetShowing cancelButtonIndex] animated:YES]; sheetShowing = nil; } else{ // I am free to act on the button push. // Just for demo.. So: sheetShowing = [[UIActionSheet alloc] initWithTitle:button.title delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Destroy" otherButtonTitles:nil, nil]; [sheetShowing showFromBarButtonItem:button animated:YES]; } } -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ sheetShowing = nil; // Respond to action sheet like normal } -(void)viewDidUnload{ [self setOneButton:nil]; [self setTwoButton:nil]; [super viewDidUnload]; } @end 

and finally a screenshot of .xib (shown in iPhone mode for image size):

enter image description here

Just plug in the button sockets and connect both button actions to the targetForBothButtons: method.

You will see that if one of the action sheets is displayed, pressing any button on the panel will dismiss the action sheet.

+2
source

Source: https://habr.com/ru/post/903016/


All Articles