简体   繁体   English

UIActionSheet取消按钮奇怪的行为

[英]UIActionSheet cancel button strange behaviour

I have a UIBarButtonItem opening an action sheet to offer users choices about what to do. 我有一个UIBarButtonItem打开一个动作表,为用户提供有关做什么的选择。 Everything works as expected unless I try to click on the "Cancel" button. 除非我尝试点击“取消”按钮,否则一切都按预期工作。 The target of the button appears to have moved up from where it should be. 按钮的目标似乎已从应该的位置向上移动。 I can only activate it by clicking somewhere in the middle of the "Cancel" and "Ok" buttons. 我只能通过单击“取消”和“确定”按钮中间的某个位置来激活它。

I've tried at action sheets in other applications and they work fine, so it's not just my big thumb. 我已经尝试过在其他应用程序中的动作表,它们工作正常,所以它不仅仅是我的大拇指。 The action sheet is opening in a UIViewController 操作表在UIViewController中打开

- (void)showOpenOptions
{
    UIActionSheet *sheet = [[UIActionSheet alloc] 
    initWithTitle:NSLocalizedString(@"Open link in external application?", @"Open in external application")
    delegate:self
    cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel")
    destructiveButtonTitle:NSLocalizedString(@"Open Link", @"Open Link")
    otherButtonTitles:nil];

    [sheet showInView:self.view];
    [sheet release];
}

Instead of passing the current view controller's view to the action sheet, use the showFromTabBar: method of UIActionSheet . 不要将当前视图控制器的视图传递给操作表, showFromTabBar:使用UIActionSheetshowFromTabBar:方法。

The Right Way 正确的方式
This will give the correct tappable area: 这将给出正确的可点击区域:

[actionSheet showFromTabBar:self.tabBarController.tabBar];

The Wrong Way 错误的方法
This will put the tappable area in the wrong place (if you're using a tab bar or toolbar): 这会将可点击区域放在错误的位置(如果您使用的是标签栏或工具栏):

[actionSheet showInView:self.view];

If you're using a toolbar , use the showFromToolbar: method instead. 如果您使用的是工具栏 ,请改用showFromToolbar:方法。 You'll need a reference to the toolbar, most likely an ivar 你需要一个工具栏的引用,很可能是一个ivar

[actionSheet showFromToolbar:self.myToolbar];

My Old Answer Also works, but is hacky: 我的旧答案也有效,但是很黑:

Just found a possible answer: 刚刚找到一个可能的答案:

01-Dec-2008 10:22 PM Tom Saxton: I looked at this bug some more, and it seems to be an issue with the tabbar. 2008年12月1日下午10:22 Tom Saxton:我再看一下这个bug,这似乎与tabbar有关。

If you call UIActionSheet's [sheet showInView:self.view] from a view controller that is a child of a UITabViewController, then the hit testing on the cancel button fails in that portion of the UIActionSheet that lies above the tabbar's view. 如果从作为UITabViewController的子视图的控制器中调用UIActionSheet的[sheet showInView:self.view],则取消按钮上的命中测试将失败在位于tabbar视图上方的UIActionSheet部分。

If you instead pass in the UITabBarController's view, then the UIActionSheet acts as expected. 如果您改为传入UITabBarController的视图,那么UIActionSheet将按预期运行。

NOTE: in iPhone OS 2.1 and earlier, the UIActionSheet came up from the top of the tab bar when you pass the child view, but in 2.2, it comes up from the bottom of the tab bar, and thus covers the tab view. 注意:在iPhone OS 2.1及更早版本中,当您传递子视图时,UIActionSheet从选项卡栏的顶部出现,但在2.2中,它从选项卡栏的底部出现,因此覆盖了选项卡视图。

http://openradar.appspot.com/6410780 http://openradar.appspot.com/6410780

Edit: It works correctly when I change the view to be the tab bar's view 编辑:当我将视图更改为标签栏的视图时,它可以正常工作

[sheet showInView:self.parentViewController.tabBarController.view];

I found an answer over here that works. 我找到了答案在这里工作的。

using: [filterActionSheet showInView:[self.view window]] ; 使用: [filterActionSheet showInView:[self.view window]] ;

i tried a few ways to get to my tab bar and they way this app is set up it seem convoluted... 我尝试了几种方法来到我的标签栏,他们的方式这个应用程序设置它似乎令人费解......

而是使用:

[sheet showFromTabBar:theTabBar];

这是修复。试试这个:

[actionsheet showInView:[UIApplication sharedApplication].keyWindow];

I think a combination of three of the answers is the right way of handling this: 我认为三个答案的组合是正确的处理方式:

    [actionSheet showFromTabBar:self.tabBarController.tabBar];

ie, use showFromTabBar (that's why it exists) and you don't need the parentViewController as Nathan pointed out (in fact, self.parentViewController.tabBarController.tabBar returns nil for me. 即,使用showFromTabBar(这就是为什么它存在),你不需要像Nathan指出的parentViewController(事实上,self.parentViewController.tabBarController.tabBar为我返回nil。

FYI - had the same problem with UIDocumentInteractionController's actionsheet stepping on the tabbar. 仅供参考 - UIDocumentInteractionController的操作表踩在标签栏上有同样的问题。 Used the following to fix. 使用以下内容进行修复。

UIViewController *parentView = [[self parentViewController] parentViewController];
[docController presentOptionsMenuFromRect: rect inView: parentView.view animated:YES];

write simplite code 编写简化代码

 actionSheet.actionSheetStyle = UIActionSheetStyleDefault;

this work fine 这项工作很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM