-2
-(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.

Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
ibamboo
  • 1,467
  • 13
  • 15

3 Answers3

4

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
  }
}
Pancho
  • 4,099
  • 1
  • 21
  • 32
1

The method is buttonTitleAtIndex: and the info is simply found on the UIAlertView doc page.

https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html#//apple_ref/occ/instm/UIAlertView/buttonTitleAtIndex:

I advise you to refer to the doc as often as you can this is how you will learn and remember.

Starscream
  • 1,128
  • 1
  • 9
  • 22
0

Actually, if you want to use the delegate method you have to use -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex.

and not

-(void)alertView:(UIAlertView*)alertView didDismissWithButton At Index:(NSInteger)buttonIndex

see: https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html#//apple_ref/occ/intfm/UIAlertViewDelegate/alertView:didDismissWithButtonIndex:

I guess it was just a typo of Pancho.

manizzo
  • 39
  • 1