2009-12-23 205 views
3

我的AlertView與「cancelButtonTitle:@」取消「」和「otherButtonTitles:nil」完美配合。我的問題是如何獲得其他按鈕。如何創建「otherButtonTitles」?

當我只更改「otherButtonTitles:@」2nd Button「」,那麼iPhone模擬器剛剛崩潰了應用程序並進入主屏幕。

回答

10

要結束你的方法調用是這樣的:

... cancelButtonTitle:@"Cancel" otherButtonTitles:@"Button1Title", @"Button2Title", nil];

這是你看到同樣的模式在字符串格式化,其中的參數列表可以是任意長度的。通常參數列表然後以零結尾。不要忘記零。

+0

不知道關於零。完美的作品,謝謝! – Devoted 2009-12-23 23:55:36

6

確切like Kevin said,但作爲附錄,您還可以將目標操作分配給其他按鈕。

當你實例化UIAlertView中,delegate參數設置爲self,然後添加下面的方法到對象:

-(void) alertView: (UIAlertView *) alertView 
     clickedButtonAtIndex: (NSInteger) buttonIndex { 
     // do stuff 
     // if you want the alert to close, just call [ alertView release ] 
} 

`

+0

如何控制哪個按鈕將激活此方法?我試了一下,無論按哪個按鈕(包括取消),它都會運行這個方法。噢噢噢!我在這個方法中設置了一個條件來檢查哪個按鈕來做什麼? – Devoted 2009-12-23 23:58:45

+0

是的。你說對了。 – 2009-12-24 00:12:09

+0

像'[self performSelector:[buttonActions objectAtIndex:buttonIndex]];' 這將工作,如果你有一個名爲'buttonActions'的選擇器 - 只是一個例子。 – 2009-12-24 00:13:59