There are a few different ways you could do this. You could use an if statement
which creates different UIAlertView
s 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