【问题标题】:UIActionSheet in Landscape has incorrect buttonIndicies横向 UIActionSheet 的 buttonIndicies 不正确
【发布时间】:2011-08-10 08:08:44
【问题描述】:

我有一个操作表,它在横向方向的 iphone 上让我感到悲伤。一切都显示得很好,但在 Landscape 中,第一个真正的按钮与取消按钮具有相同的索引,因此逻辑不起作用。

我尝试使用 initWithTitle:delegate:cancelButtonTitle:破坏性ButtonTitle:otherButtonTitles:创建actionSheet,但那是一样的,我当前的代码如下;

UIActionSheet* actionMenu = [[UIActionSheet alloc] init];

actionMenu.delegate = self;
actionMenu.title = folderentry.Name;
actionMenu.cancelButtonIndex = 0;

[actionMenu addButtonWithTitle:NSLocalizedString(@"str.menu.cancel",nil)];

[self addActiveButtons:actionMenu forEntry:folderentry];
[actionMenu showInView:[self.navigationController view]];
[actionMenu release];

addActiveButtons 方法基本上是使用这样的代码配置要添加的按钮;

[menu addButtonWithTitle:NSLocalizedString(@"str.menu.sendbyemail",nil)];

有时可能有 6 个按钮,所以在横向模式下,actionSheet 会像这样显示;

我的代表是这样回应的;

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

NSLog(@"Cancel Button Index is : %d",actionSheet.cancelButtonIndex);
NSLog(@"Button clicked was for index : %d",buttonIndex);

NSString *command = [actionSheet buttonTitleAtIndex:buttonIndex];

DLog(@"COMMAND IS: %@ for index: %d",command,buttonIndex);

if ([command isEqualToString:NSLocalizedString(@"str.menu.sendbyemail",nil)]) {

    // Do stuff here

}

if ( ... similar blocks ... ) { }

}

在显示的示例中,我发现 cancelButtonIndex 符合预期为 0,但第一个其他按钮的按钮索引也是如此!这意味着如果我点击第二个(保存到照片)按钮,我的调试输出如下所示;

取消按钮索引为:0

点击的按钮用于索引:1

命令是:通过电子邮件发送索引:1

我已经尝试了各种排列方式,现在我想知道我错过了什么。我已经进行了很好的搜索,但人们似乎遇到的其他问题是显示问题,而不是功能问题。

谁能看出我哪里出错了?

PS。我知道这不是最好的 UI 体验,但我认为大多数用户实际上大部分时间都是纵向的或使用应用程序的 iPad 版本,所以我准备接受横向的操作表默认行为,假设我可以让它真正发挥作用!

【问题讨论】:

    标签: iphone ios landscape uiactionsheet


    【解决方案1】:

    好的,通过计算我添加了多少个按钮然后添加取消按钮作为最后一个选项来修复它,所以我的代码如下所示;

    int added = [self addActiveButtons:actionMenu forEntry:folderentry];
    
    [actionMenu addButtonWithTitle:NSLocalizedString(@"str.menu.cancel",nil)];
    actionMenu.cancelButtonIndex = added;
    

    希望能帮助遇到同样问题的其他人!

    【讨论】:

    • 只是添加一个说明:要使用此技术,您需要将 nil 作为取消按钮标题传递给 initWithTitle:delegate:cancelButtonTitle:破坏性ButtonTitle:otherButtonTitles: 方法。然后您可以按照此答案中的说明最后添加取消按钮。
    • 刚刚遇到了同样的问题。提交错误:rdar://11361874 或 openradar.me/11361874
    【解决方案2】:

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

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

    对于 iPhone,N 为纵向方向的 7 和横向方向的 5。这些数字适用于所有按钮,包括 Cancel 和 Destructive。

    您最初将“取消”和“破坏性”按钮放在操作表中的哪个位置并不重要。达到限制后,Destructive 按钮将移至顶部,而 Cancel 按钮将移至底部。

    问题是指数没有相应调整。因此,如果您最初没有将 Cancel 作为最后一个按钮并将 Destructive 作为第一个按钮添加,则会在 actionSheet:clickedButtonAtIndex: 中报告错误的索引:如初始报告所述。

    因此,如果您要在操作表中包含 N 个以上的按钮,则必须将 Destructive 按钮添加到操作表,作为操作表的第一个按钮。您必须添加取消按钮作为添加到操作表的最后一个按钮。最初构建工作表时,只需将两者都保留为 nil,如另一个答案中所述。

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题。为了解决这个问题,我只是为所有按钮创建了一个带有 nil 的 actionSheet,然后手动添加了按钮。最后,在处理程序中,忽略 firstOtherButtonIndex,因为它会出错(即使您提前设置了它)。相反,假设它是 1,因为索引 0 是本示例中的取消按钮。代码如下:

      NSArray *items = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];
      UIActionSheet* actionSheet = [[[UIActionSheet alloc] initWithTitle:@"Title" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil] autorelease];
      [actionSheet addButtonWithTitle:@"Cancel"];
      for (NSString *title in items) {
          [actionSheet addButtonWithTitle:title];
      }
      [actionSheet addButtonWithTitle:@"Destroy"];
      
      // set these if you like, but don't bother setting firstOtherButtonIndex.
      actionSheet.cancelButtonIndex = 0;
      actionSheet.destructiveButtonIndex = [items count]+1;
      

      此外,如果您使用的是 iPhone,请不要忘记从标签视图中显示此内容,因为标签栏会窃取触摸事件并防止点击下方按钮。

      【讨论】:

        【解决方案4】:

        我的解决方案是像这样初始化,只指定破坏性按钮标题...

            UIActionSheet * as =[[[UIActionSheet alloc] initWithTitle:nil
                                                         delegate:self 
                                                cancelButtonTitle:nil 
                                           destructiveButtonTitle:@"Cancel" 
                                                otherButtonTitles:nil] autorelease];
        [as addButtonWithTitle:@"Button 1"];
        [as addButtonWithTitle:@"Button 2"];
        

        这样,您始终会在索引 0 处获得取消按钮,而您自己的按钮从索引 1 开始,即使存在滚动视图。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-22
          • 1970-01-01
          相关资源
          最近更新 更多