2011-03-10 39 views
25

在iPhone上使用UIActionSheet(iOS 4.2)時,我發現了一個奇怪的問題。考慮以下代碼:使用多於6個自定義按鈕時UIActionSheet buttonIndex值有問題?

- (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用於按鈕「one」等等。如果您在添加第七個按鈕的行中註釋,則該操作表會生成一種tableview,並在額外的行上添加cancel按鈕。如果在這種情況下按下「one」按鈕,buttonindex變量是0,但cancelButtonIndex也是。無法判斷用戶是否點擊了「取消」或「一個」按鈕。這似乎不應該是這樣。有人不同意嗎?謝謝你的幫助。

+0

動作表上的六個按鈕?真?我建議使用表的選項...... – Eimantas 2011-03-10 16:12:33

+9

那麼如果你不應該使用它,那麼在scrollview中有一個可以處理多於6個按鈕的動作表有什麼意義? – huesforalice 2011-03-10 16:17:43

+0

有同樣的問題,這是一個明顯的錯誤 – 2013-04-16 10:26:49

回答

34

即使我已經包括取消按鈕作爲操作表中的最後一個並相應地設置其索引,我遇到了同樣的問題。我的問題與'破壞性'按鈕有關。經過一番調查,這裏是我對這個問題:

  • N之後按鈕已被添加到actionsheet,切換它的佈局把破壞性的按鈕,在頂部和底部的取消按鈕。中間是包含所有其他按鈕的可滾動視圖。其他來源表明這是一個表格視圖。

  • N對於縱向取向爲7,對於橫向取向爲5。對於所有按鈕,包括取消和破壞性,N爲9,對於所有按鈕,包括取消和破壞性,這些數字是N,N是開關之前按鈕的最大數量,N + 1按鈕使UIActionSheet切換到可滾動的視圖

  • 在操作表中最初放置取消和破壞性按鈕的位置並不重要,一旦達到限制,破壞性按鈕移動到頂部,取消移動到底部

  • 問題是索引沒有相應的調整所以,如果你最初沒有添加取消作爲最後一個按鈕和破壞作爲第一,錯誤的索引將被報告在actionSheet :clicke dButtonAtIndex:如初始報告所述。

  • 所以,如果你的動作表中有超過N個按鈕,你必須在actionSheet中添加破壞性按鈕作爲第一個按鈕。您必須添加取消按鈕作爲添加到操作表的最後的按鈕。當最初構建工作表時,只需將兩者都設爲零,如另一個答案中所述。

+1

這是實際上正確的答案 - 不是上面標記的答案。感謝Rich發帖。對於讀者,請注意關於按鈕排序的第四個項目符號。 – Christopher 2012-04-21 06:44:20

+5

這是很荒謬的行爲。我經常想知道蘋果爲什麼做出某些決定。這是其中之一。謝謝你的提示。 – stephenmuss 2012-10-24 04:42:06

13

我剛剛遇到了這個問題。通過最初不設置取消按鈕來解決這個問題。我設置了單獨的按鈕是這樣的:

for(int index = 0; index < buttonTotal; index++) 
    { 
    [actionSheet addButtonWithTitle:[NSString stringWithFormat:buttonText, [buttonItems objectAtIndex: index]]]; 
    } 

    [actionSheet addButtonWithTitle:@"Cancel"]; 
    actionSheet.cancelButtonIndex = actionSheet.numberOfButtons; 

我相信零索引用於由destructiveButton如果你使用它,其他按鈕將有增加,否則會從0

啓動不確定我是否同意超過一定數量的表格選項,這些按鈕默認爲可滾動列表。

+0

謝謝Stefaan! – huesforalice 2011-03-11 06:59:04

+18

你應該做'actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@「Cancel」];' – user102008 2011-09-20 03:14:34

+0

本地化會不會破解這個解決方案? – 2013-04-16 10:29:01

1

file a bug關於這個問題。包括一個小樣本項目,並等待幾個月才能收到回覆。

現在可以靜態設置你的按鈕在init

UIActionSheet *actionSheet = [[UIActionSheet alloc] 
           initWithTitle:@"TestSheet" 
           delegate:self 
           cancelButtonTitle:@"Cancel" 
           destructiveButtonTitle:nil 
           otherButtonTitles: @"one", @"two", @"three", @"four", @"five", @"six", @"seven", nil]; 

作品沒有問題。

+0

感謝您的提示,我需要設置數組中的按鈕,但上面只是一個例子指出addButtonWithTitle方法似乎不起作用。 – huesforalice 2011-03-11 06:54:19

0

不喜歡如下:

UIActionSheet *actionSheet = [[UIActionSheet alloc] 
           initWithTitle:@"TestSheet" 
           delegate:self 
           cancelButtonTitle:@"Cancel" 
           destructiveButtonTitle:@"Delete" 
           otherButtonTitles: @"one", @"two", @"three", @"four", @"five", @"six", @"seven", @"eight", nil]; 

這意味着定義破壞性按鈕destructiveButtonTitle。避免在otherButtonTitles中使用破壞性/取消按鈕。

1

1)實例化使用

所述的ActionView [[UIActionSheet的alloc] InitWithTitle:委託:cancelButtonTitle:destructiveButtonTitle:OtherButtonTitles:]

沒有任何取消按鈕或破壞性按鈕(設置它們至零)

2)使用[myActionSheet addButtonWithTitle:(NSString *)]將所有按鈕添加爲正常。 3)如果你想要一個特殊的按鈕,使用與步驟2相同的方法添加它,並將標題設置爲任意值(例如@"Cancel")。

4)現在將屬性[myActionSheet setCancelButtonIndex:]設置爲操作表中最後一個按鈕的索引,即取消按鈕。對破壞性按鈕做同樣的事情。 (這些是-1默認,這會導致他們不被示出)

  • 注意破壞性按鈕將始終顯示在上面,以及取消按鈕將總是出現在底部,這是不能被改變。

  • 另外,您可以在索引0處添加取消/破壞性按鈕,然後添加所有其他按鈕。但是現在,其他按鈕的第一個索引是1,而不是零。如果您有一個與alertview按鈕相對應的數組,可能會引起混淆。