2011-10-06 57 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; 
} 
相關問題