2011-02-06 15 views
18

¿Cómo puedo descartar UIAlertView? Este código no funciona.¿Cómo puedo descartar UIAlertView?

@property (nonatomic, retain) UIAlertView *activityAlertView; 
- (void)viewDidLoad 
{ 
self.activityAlertView = [[UIAlertView alloc] initWithTitle:@"Receiving data" message:@"\n\n" 
                 delegate:self 
               cancelButtonTitle:@"Cancel" 
               otherButtonTitles:nil, nil]; 
[activityAlertView show]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
} 

-(void) myfunc 
{ 
[self alertView:activityAlertView clickedButtonAtIndex:1]; 
} 

Respuesta

56

El método - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated de la clase UIAlertView hace lo que quiere. por ejemplo:

[myAlertView dismissWithClickedButtonIndex:-1 animated:YES]; 
+1

Thnx Buddy ... :-) – Ayaz

+4

Probablemente sea más limpio utilizar el índice del botón Cancelar: '[myAlertView dismissWithClickedButtonIndex: myAlertView.cancelButtonIndex animated: YES];'. –

+0

@VincentTourraine Ouh, vieja respuesta que desenterraste allí. Si el índice del botón Cancelar es el correcto, depende del comportamiento que desee. Si solo desea descartar la alerta, '-1' es el camino a seguir. Si prefiere ejecutar el caso de cancelación, el índice del botón Cancelar es correcto. – JustSid

5

Puesto que usted está usando un método delegado de devolución de llamada de UIAlertView, creo que es mejor que utilice el siguiente código

[myAlertView dismissWithClickedButtonIndex:0 animated:YES]; 

si no, utilizar el código sugerido anteriormente

[myAlertView dismissWithClickedButtonIndex:-1 animated:YES]; 
+1

dismissWithClickedButtonIndex – Gank