I have this 2-button alert view:
UIAlertView* message = [[UIAlertView alloc]
initWithTitle: @"Delete?" message: @"This business will be deleted permenently." delegate: nil
cancelButtonTitle: @"Cancel" otherButtonTitles: @"Delete", nil];
[message show];
I also have this method:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"Delete"])
{
NSLog(@"Button DELETE was selected.");
}
else if([title isEqualToString:@"Cancel"])
{
NSLog(@"Button CANCEL was selected.");
}
}
and I added this to the .h file:
<UIAlertViewDelegate>
Right now when either button is pressed, it just closes the dialog. That is ok for cancel, but how do I know when the delete button is pressed?
Thanks!
You have to implement the
– alertView:clickedButtonAtIndex:method of the UIAlertViewDelegate. You also have to set the delegate when initialising the alert view.E.g.
The cancel button’s index is 0.