在iPad上从UIBarButtonItem中删除UIActionSheet

时间:2011-12-03 21:03:53

标签: ios ipad uibarbuttonitem uiactionsheet

我有UIToolbarUIBarButtonItemUIActionSheet,使用showFromBarButtonItem:显示各种UIBarButtonItem

在iPad上,当其中一个动作表出现在屏幕上时,触摸动作表外的任何位置都会将其删除而不执行任何操作(例如,触摸按钮不会触发按钮)。这是设计 - 我不满意的东西,但我接受它,只要它是通常的行为。

但有一个例外。如果我触摸另一个UIActionSheet,则会触发此按钮,并且不会从屏幕上删除当前的操作表。如果新按钮恰好启动另一个{{1}},我最终会在屏幕上显示两个(或更多)操作表。

当然,我可以通过一个繁琐的过程来记住屏幕上的内容并手动解除它,但我也关注用户,因为一些触摸(那些以工具栏按钮为目标)被尊重而其他人被忽略

在这种情况下,我能做些什么或做什么?

1 个答案:

答案 0 :(得分:2)

这似乎是一个恼人的不一致,但不是一个难以纠正。此示例假设您只需要显示UIActionSheets,就像您的情况一样。

以下是一个有关如何解决此问题的功能示例,从.h

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

.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

最后是.xib的屏幕截图(在iPhone模式下显示图片大小):

enter image description here

只需连接按钮插座,然后将两个按钮操作连接到targetForBothButtons:方法。

您会看到,如果其中一个操作表显示,则按任意栏按钮将导致解除操作表。

相关问题