2014-02-06 62 views
-4

是否有可能根據不同的條件在同一個IBAction中用不同的按鈕顯示不同的警報?我在IBAction爲一些代碼:如何在一個IBAction中使用不同的按鈕使用多個UIAlertView?

- (IBAction)btnRecommendationTapped:(id)sender { 
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Recommendation" 
                 message: @"It seems that you are interested in the %d of this collection." 
                delegate: nil 
              cancelButtonTitle: nil 
              otherButtonTitles: @"Yes", @"No", nil]; 
[alert show]; 
} 

我想有另一個警報顯示「!很抱歉,我們無法提供任何建議,」用確定按鈕。當條件滿足時,顯示IBAction中的一個顯示這個。

+3

'if(condition){create alertView} else {create other alertView}'?! –

+0

條件是什麼?你聽說過「if語句」嗎? – Popeye

回答

2
-(IBAction)buttonClicked:(id)sender 
    { 
      if([sender tag]==0) 
     { 
      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"button 1 clicked" delegate:self 
      cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
       [alert show]; 
     } 
     else if ([sender tag]==1) 
     { 
      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"button 2 clicked" delegate:self 
      cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
       [alert show]; 
     } 
     else if ([sender tag]==2) 
     { 
      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"button 3 clicked" delegate:self 
      cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
       [alert show]; 
     } 
} 
+1

公約說'ButtonClicked:'應該是'buttonClicked:' – Popeye

+0

@Popeye謝謝我編輯我的答案 –

0

嘗試如下:

- (IBAction)btnRecommendationTapped:(id)sender { 
if(condition has fulfilled){ 
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Recommendation" 
                 message: @"It seems that you are interested in the %d of this collection." 
                delegate: nil 
              cancelButtonTitle: nil 
              otherButtonTitles: @"Yes", @"No", nil]; 
[alert show]; 
} 
else{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Sorry! we are unable to provide any recommendation" 
                 message: nil" 
                delegate: nil 
              cancelButtonTitle: nil 
              otherButtonTitles: @"Ok", nil]; 
[alert show]; 

} 
} 
+0

如果不是你的答案是錯誤的,那麼額外的'''是一個錯字 – Popeye

1

有幾種不同的方法可以做到這一點。您可以使用if statement,根據條件創建不同的UIAlertView

選項1:

- (IBAction)btnClicked:(id)sender 
{ 
    UIAlertView *ourAlertView; // Create the local variable, we don't need to create multiple ones we can just set to the one. 
    // This is your if statement to determine if your condition has been met 
    // Whatever that condition is here we're going to assume you have set a BOOL 
    // flag before hand called failed. 
    if(!failed) { 
     ourAlertView = [[UIAlertView alloc] initWithTitle:@"Recommendations" 
                message:@"We recommend..." 
               delegate:self // Very important 
             cancelButtonTitle:@"No" 
             otherButtonTitles:@"Yes", nil]; 
     [ourAlertView setTag:0]; // Would recommend setting a tag to determine which one has been used in the delegate methods. 
    } else { // else if() if you want more then 2 
     ourAlertView = [[UIAlertView alloc] initWithTitle:@"Sorry" 
                message:@"We're sorry..." 
               delegate:self // Very important 
             cancelButtonTitle:@"OK" 
             otherButtonTitles:nil]; 
     [ourAlertView setTag:1]; 
    } 

    [ourAlertView show]; 
} 

第二種辦法很簡單,但我們會做以下,仍使用if statement但我們只建立一個UIAlertView

選項2:

- (IBAction)btnClicked:(id)sender 
{ 
    // We create an empty `UIAlertView` only setting the delegate to self. 
    UIAlertView *ourAlertView = [[UIAlertView alloc] initWithTitle:nil 
               message:nil 
               delegate:self // Very important 
            cancelButtonTitle:nil 
            otherButtonTitles:nil]; 

    if(!failed) { 
     [ourAlertView setTitle:@"Recommendations"]; 
     [ourAlertView setMessage:@"We recommend..."]; 
     [ourAlertView addButtonWithTitle:@"No"]; 
     [ourAlertView addButtonWithTitle:@"Yes"]; 
     [ourAlertView setCancelButtonIndex:0]; 
     [ourAlertView setTag:0]; 
    } else { 
     [ourAlertView setTitle:@"Sorry"]; 
     [ourAlertView setMessage:@"We're sorry..."]; 
     [ourAlertView addButtonWithTitle:@"OK"]; 
     [ourAlertView setCancelButtonIndex:0]; 
     [ourAlertView setTag:1]; 
    } 

    [ourAlertView show]; 
} 

但是最重要的需要的東西(那就是,如果你想讓它正常工作),很多人忘記的是UIAlertViewDelegate

所以在你.h文件,你必須在@interface設置你需要做的

@interface MySubClass : MySuperClass <UIAlertViewDelegate> 

這將允許您使用以下方法從UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex 
- (void)alertViewCancel:(UIAlertView *)alertView 
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *) 
- (void)didPresentAlertView:(UIAlertView *)alertView 
- (void)willPresentAlertView:(UIAlertView *)alertView 

結帳兩個UIAlertView Apple DocumentationUIAlertViewDelegate Apple Documentation

非常重要否關於UIAlertView的蘋果文檔中有關子類化的問題。

UIAlertView類旨在按原樣使用,不支持子類。這個類的視圖層次是私有的,不能修改

+0

感謝您的回覆!非常詳細和有用! – Richard

相關問題