-4

Is it possible to show different alerts with different buttons in the same IBAction based on different conditions?? I have some code in IBAction:

- (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];
}

I would like to have another alert shows "Sorry! we are unable to provide any recommendation" with a ok button. when the condition has fulfilled shows the one in IBAction otherwise shows this one.

Nikos M.
  • 13,685
  • 4
  • 47
  • 61
Richard
  • 125
  • 1
  • 9

3 Answers3

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];
         }
}
Bug
  • 2,576
  • 2
  • 21
  • 36
1

There are a few different ways you could do this. You could use an if statement which creates different UIAlertViews based on the condition.

Option 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];
}

The second option would very simpler but we would do the following, still using an if statement but we only create one UIAlertView

Option 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];
}

But the most important thing that is required (That is if you want it to work properly) that a lot of people forget about is the UIAlertViewDelegate.

So in your .h file where you have the @interface set you need to do

@interface MySubClass : MySuperClass <UIAlertViewDelegate>

this will allow you to use the following methods from the 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

Checkout both the UIAlertView Apple Documentation and the UIAlertViewDelegate Apple Documentation

Very Important Note about subclassing from Apple documentation on UIAlertView you most comply too.

The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified

Popeye
  • 11,839
  • 9
  • 58
  • 91
0

try as follow:

- (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];

}
}
Mani
  • 305
  • 2
  • 9