I am trying to perform the smile task of having two alerts in a single View Controller. The code below works fine, but how would I make another instance of it elsewhere in the View Controller. I am concerned that if I duplicated the code, my buttonIndex would not know which alert it is responding to. Any ideas? Thanks!
-(void)alertChoice
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
message:@"Message" delegate:self
cancelButtonTitle:@"Cancel" otherButtonTitles:@"Confirm", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
//do something
}
}
You can use the tag
property on UIAlertView
to decipher which alert is which:
-(void)alertChoice
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
message:@"Message" delegate:self
cancelButtonTitle:@"Cancel" otherButtonTitles:@"Confirm", nil];
alert.tag = 0;
[alert show];
}
-(void)alertChoice1
{
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Title"
message:@"Message" delegate:self
cancelButtonTitle:@"Cancel" otherButtonTitles:@"Confirm", nil];
alert1.tag = 1;
[alert1 show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag == 0)
{
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With