开发者

UIAlertView easy way to tell if cancel button is selected

开发者 https://www.devze.com 2023-03-24 18:48 出处:网络
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 开

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?


The UIAlertView has a property of cancel button index

@property(nonatomic) NSInteger cancelButtonIndex

Usage

[alertView cancelButtonIndex]


- (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)
}


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...
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消