【问题标题】:Dismissing UIActionSheet from UIBarButtonItem on an iPad从 iPad 上的 UIBarButtonItem 解除 UIActionSheet
【发布时间】:2011-12-04 20:26:11
【问题描述】:

我有一个UIToolbar 和几个UIBarButtonItems,它们使用showFromBarButtonItem: 显示各种UIActionSheets。

在 iPad 上,当其中一个操作表出现在屏幕上时,触摸操作表之外的任何位置都会将其移除,并且不会执行任何其他操作(例如,触摸按钮不会触发按钮)。这是设计使然 - 我对此并不满意,但只要这是通常的行为,我就接受它。

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

当然,我可以经历一个繁琐的过程来记住屏幕上的内容并手动将其关闭,但我也担心用户,因为某些触摸(针对工具栏按钮的那些)会得到尊重,而另一些会被忽略.

有什么我可以做或者我必须忍受这种情况的吗?

【问题讨论】:

    标签: ios ipad uibarbuttonitem uiactionsheet


    【解决方案1】:

    这似乎是一个令人讨厌的不一致,但并不难纠正。此示例假设您只需要显示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 模式显示的图像大小):

    只需连接按钮插座并将两个按钮操作都连接到targetForBothButtons: 方法。

    您会看到,如果其中一个操作表正在显示,那么按下任何条形按钮都会导致该操作表消失。

    【讨论】:

    • 谢谢。我担心这是唯一的选择,但我确实错过了 cancelButtonIndex 属性,这让事情变得更容易了。
    猜你喜欢
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多