使用6个以上的自定义按钮时,UIActionSheet buttonIndex值是否有问题? [英] UIActionSheet buttonIndex values faulty when using more than 6 custom buttons?

查看:198
本文介绍了使用6个以上的自定义按钮时,UIActionSheet buttonIndex值是否有问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iPhone(iOS 4.2)上使用UIActionSheet时发现了一个奇怪的问题。考虑以下代码:

I have discovered a strange problem when using UIActionSheet on the iPhone (iOS 4.2). Consider this code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    [self.window addSubview:viewController.view];
    [self.window makeKeyAndVisible];

    UIActionSheet *actionSheet = [[UIActionSheet alloc] 
                                  initWithTitle:@"TestSheet" 
                                  delegate:self 
                                  cancelButtonTitle:@"Cancel" 
                                  destructiveButtonTitle:nil 
                                  otherButtonTitles: nil];

    [actionSheet addButtonWithTitle:@"one"];
    [actionSheet addButtonWithTitle:@"two"];
    [actionSheet addButtonWithTitle:@"three"];
    [actionSheet addButtonWithTitle:@"four"];
    [actionSheet addButtonWithTitle:@"five"];
    [actionSheet addButtonWithTitle:@"six"];
    //uncomment next line to see the problem in action
    //[actionSheet addButtonWithTitle:@"seven"];

    [actionSheet showInView:window];
    [actionSheet release];

    return YES;
}
- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSLog(@"buttonIndex: %d, cancelButtonIndex: %d, firstOtherButtonIndex: %d",
          buttonIndex, 
          actionSheet.cancelButtonIndex, 
          actionSheet.firstOtherButtonIndex);
}

如果启动此应用程序,操作表将按预期运行。这意味着cancelButtonIndex始终为0,并正确报告按钮索引。按钮1等等1。如果您在添加第七个按钮的行中注释,则操作表会生成一种tableview,并在额外的行上显示取消按钮。如果我在这种情况下按一按钮,则buttonindex变量为0,但cancelButtonIndex也是如此。无法判断用户是否已点击取消或一个按钮。这似乎不应该是这样的。有人不同意吗?谢谢你的帮助。

If you start this application, the actionsheet behaves as expected. That means the cancelButtonIndex is always 0, and the button indexes are reported correctly. 1 for button "one" and so on. If you comment in the line for adding the seventh button, the actionsheet produces a sort of tableview, with the cancel button on an extra line. If I press the "one" button in this case, the buttonindex variable is 0, but so is the cancelButtonIndex. It is impossible to tell if the user has tapped the "cancel" or the "one" button. That doesn't seem like it should be this way. Does anyone disagree? Thanks for your help.

推荐答案

我遇到了同样的问题,即使我已经将取消按钮包含在最后一个操作表并相应地设置其索引。我的问题与破坏性按钮有关。经过一番调查,这是我对这个问题的看法:

I ran into the same issue even though I already was including the Cancel Button as the last one in the action sheet and setting its index accordingly. My problems had to do with the 'Destructive' button. After some investigation, here is my take on the problem:


  • 将N个按钮添加到动作表后,它会切换到布局将Destructive按钮放在顶部,将Cancel按钮放在底部。中间是一个包含所有其他按钮的可滚动视图。其他来源表明这是一个表格视图。

  • After N buttons have been added to the actionsheet, it switches its layout to put the Destructive button at the top and the Cancel button at the bottom. In between is a scrollable view that includes all of the other buttons. Other sources indicate that this is a a table view.

纵向方向N为7,横向方向为5。在较大的4英寸屏幕上,N为9表示纵向。这些数字适用于所有按钮,包括取消和破坏性。要清楚,N是切换前最大的按钮数.N + 1按钮使UIActionSheet切换到可滚动视图。

N is 7 for Portrait orientation and 5 for Landscape orientation. N is 9 for Portrait orientation on larger, 4" screen. These numbers are for all buttons including Cancel and Destructive. To be clear, N is the largest number of buttons before the switch. N+1 buttons causes the UIActionSheet to switch to the scrollable view.

操作表中最初将取消和破坏性按钮放在操作表中的位置并不重要。一旦达到限制,Destructive按钮移动到顶部,Cancel移动到底部。

It does not matter where in the action sheet you had originally put the Cancel and Destructive buttons within the action sheet. Once the limit has been reached, the Destructive button is moved to the top and the Cancel is moved to the bottom.

问题是索引没有相应调整。所以,如果你没有最初添加取消作为最后一个按钮而破坏作为第一个按钮,将在actionSheet中报告错误的索引:clickedButtonAtIndex:作为初始报告说明。

The problem is that the indices are not adjusted accordingly. So, if you did not initially add the Cancel as the last button and the Destructive as the first, the wrong index will be reported in actionSheet:clickedButtonAtIndex: as the initial report stated.

因此,如果您的操作表中有超过N个按钮,则必须将毁灭性按钮添加到actionSheet,作为操作表的第一个按钮。您必须添加取消按钮作为添加到操作表的最后按钮。在最初构建工作表时,只需将两者都保留为nil,如另一个答案中所述。

So, if you are going to have more than N buttons in your action sheet you MUST add the Destructive button to the actionSheet as the first button to the action sheet. You MUST add the Cancel button as the last button added to the action sheet. When initially constructing the sheet just leave both as nil, as described in another answer.

这篇关于使用6个以上的自定义按钮时,UIActionSheet buttonIndex值是否有问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆