iosobjective-cuialertviewuialertviewdelegate

how to get UIAlertView otherButtons' title?


-(void) alertView: (UIAlertView *) alertVw clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *str = [NSString stringWithFormat:@"%d 。", buttonIndex];
    UIAlertView *newAlertVw = [[UIAlertView alloc] initWithTitle:@"INFO" message:str delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [newAlertVw show];
}


UIAlertView *alertVw = [[UIAlertView alloc] initWithTitle:@"TITLE"
                                                      message:@"box message"
                                                     delegate:self
                                            cancelButtonTitle:@"hide"
                                            otherButtonTitles:@"T1", @"T2", nil];
    [alertVw show];

if user touched the one of otherButtons, yes, we know which button that user touched. then, how can I to get the title of button that user just touched? thanks.


Solution

  • With the delegate on your UIAlertView set to self you can use this:

    -(void)alertView:(UIAlertView*)alertView didDismissWithButtonAtIndex:(NSInteger)buttonIndex
    {
      NSLog(@"Button Title -----> %@",[alertView buttonTitleAtIndex:buttonIndex]);
    
      // or you can check if it equals to string
      if([[alertView buttonTitleAtIndex:buttonIndex]isEqual:@"Enter"])
      {
        // your code goes here
      }
    }