Pregunta

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

¿Fue útil?

Solución

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

Otros consejos

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.

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top