2012-10-04 13 views
5


Tengo dos UIAlertViews con botones ok/cancelar.
capto la respuesta del usuario por:Múltiples UIAlertViews en la misma vista

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 

La pregunta que estoy teniendo es, cuales alertView está actualmente abierto?
que tienen diferentes acciones para hacer al hacer clic en Aceptar/Cancelar en cada uno ...

+0

Utilice la propiedad .tag a diferenciarse. [Esta es la pregunta que hizo] [1] [1]: http://stackoverflow.com/questions/4346418/uialertviewdelegate-and-more-alert-windows –

Respuesta

20

Tiene varias opciones:

  • Use Ivars. Al crear la vista de alertas:

    myFirstAlertView = [[UIAlertView alloc] initWith...]; 
    [myFirstAlertView show]; 
    // similarly for the other alert view(s). 
    

    Y en el método delegado:

    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 
        if (alertView == myFirstAlertView) { 
         // do something. 
        } else if (alertView == mySecondAlertView) { 
         // do something else. 
        } 
    } 
    
  • uso de la propiedad tagUIView:

    #define kFirstAlertViewTag 1 
    #define kSecondAlertViewTag 2 
    

    UIAlertView *firstAlertView = [[UIAlertView alloc] initWith...]; 
    firstAlertView.tag = kFirstAlertViewTag; 
    [firstAlertView show]; 
    // similarly for the other alert view(s). 
    

    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 
        switch (alertView.tag) { 
         case kFirstAlertViewTag: 
          // do something; 
          break; 
         case kSecondAlertViewTag: 
          // do something else 
          break; 
        } 
    } 
    
  • Subclase UIAlertView y añadir un alojamiento userInfo. De esta forma, puede agregar un identificador a sus vistas de alerta.

    @interface MyAlertView : UIAlertView 
    @property (nonatomic) id userInfo; 
    @end 
    

    myFirstAlertView = [[MyAlertView alloc] initWith...]; 
    myFirstAlertView.userInfo = firstUserInfo; 
    [myFirstAlertView show]; 
    // similarly for the other alert view(s). 
    

    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 
        if (alertView.userInfo == firstUserInfo) { 
         // do something. 
        } else if (alertView.userInfo == secondUserInfo) { 
         // do something else. 
        } 
    } 
    
1

UIAlertView es una subclase UIView esta manera puede utilizar su propiedad tag para su identificación. Así que cuando se crea la vista de alerta establecido su valor de la etiqueta y entonces será capaz de hacer lo siguiente:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{ 
    if (alertView.tag == kFirstAlertTag){ 
     // First alert 
    } 
    if (alertView.tag == kSecondAlertTag){ 
     // First alert 
    } 
} 
Cuestiones relacionadas