UIAlertView easy way to tell if cancel button is selected
19,296
Solution 1
The UIAlertView has a property of cancel button index
@property(nonatomic) NSInteger cancelButtonIndex
Usage
[alertView cancelButtonIndex]
Solution 2
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == [alertView cancelButtonIndex]) {
NSLog(@"The cancel button was clicked for alertView");
}
// else do your stuff for the rest of the buttons (firstOtherButtonIndex, secondOtherButtonIndex, etc)
}
Solution 3
In the delegate of UIAlertView is the method
(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
And then:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSInteger cancelIndex = [alertView cancelButtonIndex];
if (cancelIndex != -1 && cancelIndex == buttonIndex)
{
// Do something...
}
}
Author by
CodeGuy
Updated on July 02, 2022Comments
-
CodeGuy almost 2 years
I know I've done this before but I just can't figure it out again.
What is the method I would use to see if a cancel button was pressed. I don't want to do it based on the button index. There is a way to do it, something like:
[alertView isCancelIndex:index];
Anyone know?
-
rich almost 13 yearsThe OP specifically said not using that method
-
Hot Licks almost 13 yearsI think the OP meant that he didn't want to hard-code the index. (To the extent that he knew what he meant.)