开发者

UIAlertView button tagging

Is there a way I can add a .tag to an UIAlertView button? Reason being, I'm adding a few dynamic buttons to the alert that will sometimes be in the alert and sometimes not. I figured the best way was to add a tag. Is there a better method for this?

The options that will ALWAYS be in the alert are Ema开发者_如何转开发il, Save. And the 2 optional options are Tweet This and Facebook.

Thanks for any help in advance!


There is one method buttonTitleAtIndex for UIAlertView. Use that to find the button tapped by user.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *buttonString = [alertView buttonTitleAtIndex:buttonIndex];
    if( [buttonString isEqualToString:@"Facebook"] ){
        // your code here
    } else if( [buttonString isEqualToString:@"twitter"] ){
        // your code here
    }
}


You can also use tag proprerty:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Info"
                                                message:@"Info text"
                                               delegate:self 
                                      cancelButtonTitle:@"Ok"
                                      otherButtonTitles:nil, nil];   
[alert setTag:0]; 

Then in delegate:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {  
    switch (alertView.tag) {
      case 1: {
        ... 
        break;
      } 
      ...  
    default:
        break;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜