guys:
There is two buttons in my viewController of test app, the right one I call it "NO",
and the other one is "YES". The two buttons will call two different functions, and when
user press one of the buttons , I want show the user an alert to confirm that.
I know use the UIAlertViewDelegate
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
but there is two buttons, I am puzzled. How can I know which button is pressed.
So,pls help me with this, thank you in advance!
When you create an UIAlertView
you can set up a tag
for it
-(IBAction)yesButtonClick:(id)sender{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle: @"Cancel" otherButtonTitles:@"OK", nil];
alert.tag = 101;
[alert show];
}
-(IBAction)noButtonClick:(id)sender{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle: @"Cancel" otherButtonTitles:@"OK", nil];
alert.tag = 102;
[alert show];
}
In the delegate method check which alert is being shown
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag == 101) {
// from YES button
}
else if (alertView.tag == 102) {
// from NO button
}
}
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