I have an UIAlert
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"ok"
message:@"Canc?"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:@"Annul", nil];
[alertView show];
[alertView release];
and its delegate method:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0)//OK button pressed
{
//do something
}
else if(buttonIndex == 1)//Annul button pressed.
{
//do something
}
and it's all ok but if I have another alertview example "alertViewOne", I want that this alertViewOne have its delegate method and it shouldn't use delegate method of first alertview; how does change my code?
Simply set a tag to each Alert view and check which one sent the messeg.
alertView.tag=0;
And then
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(alertView.tag==0){
if(buttonIndex == 0)//OK button pressed
{
//do something
}
else if(buttonIndex == 1)//Annul button pressed.
{
//do something
}
}else{
if(buttonIndex == 0)//OK button pressed
{
//do something
}
else if(buttonIndex == 1)//Annul button pressed.
{
//do something
}
}
Update There is a better solution using blocks.
You can look at this project for example: UIAlertView-Blocks
And as far as I know iOS8 will come with native alerts with blocks.
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