2010-12-03 55 views
11

我有控制器實現UIAlertViewDelegate。在實施中我有:UIAlertViewDelegate和更多警報窗口

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

方法。當我創建UIAlertView時,我將'委託'放在'self'中,並且工作正常。但問題是,現在我有更多的警報視圖,我想爲他們每個人不同的行爲。那麼如何檢查哪個alertView發送消息?

回答

12

UIAlertView中是一個UIView subsclass等方面有着你可以用它們來區分標籤屬性:

UIAlertView *alert1 = ... //Create alert 
alert1.tag = kActionTag1; 
//show alert 

... 

UIAlertView *alert2 = ... //Create alert 
alert2.tag = kActionTag2; 
//show alert 

然後在委託方法:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    if (alertView.tag == kActionTag1){ 
      // Perform 1st action 
    } 
    if (alertView.tag == kActionTag1){ 
      // Perform 2nd action 
    } 
} 
+0

謝謝,這有助於很多:) – 1110 2010-12-03 14:54:51

0

指向每個特定警報視圖的指針在委託方法的alertView參數中發送。你只需要跟蹤指針(例如通過實例變量),以便知道哪個是哪個並相應地採取行動。

0

UIAlertView中氣體的標記屬性。在創建它時進行設置,並且可以檢查委託中的標記。