2011-10-06 58 views
7

我试图从iPad显示UIActionSheet。下面是我使用的代码:iPad UIActionSheet - 不显示最近添加的按钮

-(void) presentMenu { 
    UIActionSheet *popupMenu = [[UIActionSheet alloc] initWithTitle:@"Menu" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil]; 
    for (NSString *option in _menuItems) { 
     [popupMenu addButtonWithTitle:option]; 
    } 
    popupMenu.actionSheetStyle = UIActionSheetStyleBlackOpaque; 
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { 
     [popupMenu showFromTabBar:_appDelegate.tabBar.tabBar]; 
    } 
    else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
     [popupMenu showFromBarButtonItem:self.navigationItem.rightBarButtonItem animated:YES]; 
    } 
    [popupMenu release]; 
    return; 
} 

程序的iPhone版本显示_menuItems所有的按钮,但iPad版本刚刚从数组忽略了最后一个项目。有谁知道为什么会发生这种情况?

谢谢,
Teja。

+0

有多少项目在'_menuItems'有哪些? – matsr

回答

2

只要我输入这篇文章找到答案。以某种方式删除“取消”按钮导致这两个按钮出现。奇怪的。

编辑:虽然,这真的很烦人,因为我所有的按钮索引在iPhone和iPad版本之间改变(iPhone仍然需要取消按钮)。我该如何处理?

0

我认为iOS正在做的是期待最后一个按钮成为取消按钮(无论是否为),并将其删除,但可能只适用于iPad。这可能是因为用户可以点击以外的操作表来解雇它。我在苹果设计选择方面遇到的问题是,可能并不总是很明显,对话可以或应该以这种方式被解雇。

例如,我通过调用[actionSheet showInView:self.view];来显示我的操作表。这会导致整个视图变灰,操作表会显示在设备中间。在我看来,用户会 - 正确地认为 - 他们必须选择其中一个按钮。

我知道还有其他的操作表显示机制 - 就像将其显示为附加到条形按钮项目的气泡一样 - 其中取消按钮显然是多余的。如果Apple允许在这里获得更大的灵活性,那将会很不错。对于我的应用程序,我可能必须在我传入我的自定义构造函数的数组末尾添加一个虚拟按钮,并知道iOS会隐藏它。如果行为在iOS的未来版本中发生变化......那么我当时就必须解决它。

在你的情况,我建议不要使用带有cancelButtonTitle和destructiveButtonTitle的构造函数。相反,使用上面的方法手动子类UIActionSheet并手动添加按钮。然后,将cancelButtonIndex和destructiveButtonIndex设置为所需的索引。记住你不要来设置这两个属性;他们默认为-1(无按钮)。另外,请记住遵守HIG有关按钮位置的规定。

这里是我的子类的构造(编辑为简洁起见)中的一个,只是给你一个想法:

- (instancetype)initWithTitle:(NSString *)title 
       buttonTitles:(NSArray *)buttonTitles 
      cancelButtonIndex:(NSInteger)cancelButtonIndex 
     destructiveButtonIndex:(NSInteger)destructiveButtonIndex 
{ 
    self = [super initWithTitle:title delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; 

    if (self) 
    { 
     if (buttonTitles) 
     { 
      [buttonTitles enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) 
      { 
       [self addButtonWithTitle:obj]; 
      }]; 
     } 
     self.cancelButtonIndex = cancelButtonIndex; 
     self.destructiveButtonIndex = destructiveButtonIndex; 
     if (self.cancelButtonIndex > -1) 
     { 
      [self addButtonWithTitle:@""]; 
     } 
    } 

    return self; 
} 
相关问题