2014-09-20 90 views
10

在UIActionSheet的iOS8上的委託方法多次調用UIActionsheet委託方法在ios8中調用兩次?

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

我在ipad 2已檢查與IOS 8

回答

6

它的IOS 8的錯誤..

作爲@rob所述UIActionSheet是在IOS 8(UIActionSheetDelegate也棄用。)棄用

要創建並在IOS 8管理動作片和後,使用UIAlertController

+2

呃,iOS 8.0.x已經成爲iOS版本以來的最bu ... ...... iOS 4.0是什麼? – Epaga 2014-10-13 07:15:42

4

這確實是我見過的報道在幾個地方的當前行爲。它似乎是一個錯誤,希望很快就會被修復,但無法確定。

2

您應該使用UIAlertController替換iOS8中的所有AlertView和ActionSheet。 如果你的目標不是iOS8上的低,那麼你應該檢查版本,並添加更多的代碼

例如,這個代碼是iOS8上ActionSheet

-(void)testActionSheet{ 
    UIAlertController* alertAS = [UIAlertController alertControllerWithTitle:@"Test ActionSheet" 
                    message:nil 
                  preferredStyle:UIAlertControllerStyleActionSheet]; 

    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"Action" style:UIAlertActionStyleDefault 
                  handler:^(UIAlertAction * action) { 
                   NSLog(@"Action"); 
                  }]; 
    [alertAS addAction:defaultAction]; 
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { 

    }]; 
    [alertAS addAction:cancleAction]; 
    [self presentViewController:alertAS animated:YES completion:nil]; 

} 
2

如果你想繼續使用現有的API,有一點的行爲,讓你知道什麼時候運行代碼和時忽略則委託調用 - buttonIndex

第一次調用委託方法時,它們始終通過正確的buttonIndex。但是,第二次通過取消按鈕的buttonIndex來調用它們。

1

我有同樣的問題。一種解決方法:使用actionSheet.tag。在實例化時將其設置爲有效數字(默認情況下爲0),例如1,2,...。處理響應:

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex 

檢查它是否有效(例如:在處理它之前不是-1)。一旦響應這裏處理,在返回前,設置:

actionSheet.tag = -1; 

這可以確保你會忽略它,雖然第二呼叫。這適用於我的情況。

0

我想補充一點:UIAlertController是現在在iOS8.0 +中使用UIAlertViews的正確方法(因爲UIAlertView已被棄用),但是能夠選擇多個選項的錯誤有所緩解。

警報視圖中的單獨選項可以同時被選中/突出顯示,但委託方法似乎只能觸發其中一個選項。哪一個實際觸發是不確定的,但是我可以確認,如果使用多個手指,儘管突出顯示兩個或更多個,但只有一個被觸發。