2011-08-03 32 views
14

我知道我以前做過這件事,但我無法再想出來。UIAlertView簡單的方法來判斷是否選擇了取消按鈕

什麼是我會用來查看是否按下取消按鈕的方法。我不想根據按鈕索引來做。有一種方法可以做到這一點,例如:

[alertView isCancelIndex:index]; 

任何人都知道嗎?

+8

那樣的問題很容易通過文檔解決。你也會節省很多時間。它像使用UIAlertView類的參考一樣簡單,點擊Apple的鏈接,然後向下滾動到屬性和方法列表。 – EmilioPelaez

回答

52

的UIAlertView中具有取消按鈕索引

@property(nonatomic) NSInteger cancelButtonIndex 

用法

[alertView cancelButtonIndex] 
2

在UIAlertView中的代表的一個屬性是該方法

(無效)alertView:(UIAlertView中* )alertView clickedButtonAtIndex:(NSInteger)buttonIndex

然後:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSInteger cancelIndex = [alertView cancelButtonIndex]; 
    if (cancelIndex != -1 && cancelIndex == buttonIndex) 
    { 
     // Do something... 
    } 
} 
+0

該OP明確表示不使用該方法 – rich

+2

我認爲OP意味着他不想對該索引進行硬編碼。 (他知道他的意思。) –

32
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 

    if (buttonIndex == [alertView cancelButtonIndex]) { 
    NSLog(@"The cancel button was clicked for alertView"); 
    } 
// else do your stuff for the rest of the buttons (firstOtherButtonIndex, secondOtherButtonIndex, etc) 
} 

相關問題