2010-02-26 15 views
38

Actualmente tengo una clase emergente UIAlertView s aquí y allá. Actualmente, la misma clase es la delegada para estos (es muy lógico que lo sea). Desafortunadamente, estos UIAlertView s llamarán a los mismos métodos delegados de la clase. Ahora, la pregunta es: ¿cómo sabes desde qué vista de alerta se invoca un método delegado? Estaba pensando en revisar el título de la vista de alerta, pero eso no es tan elegante. ¿Cuál es la forma más elegante de manejar varios UIAlertView s?Varios UIAlertViews para un delegado

Respuesta

102

Tag las UIAlertView s como este:

#define kAlertViewOne 1 
#define kAlertViewTwo 2 

UIAlertView *alertView1 = [[UIAlertView alloc] init... 
alertView1.tag = kAlertViewOne; 

UIAlertView *alertView2 = [[UIAlertView alloc] init... 
alertView2.tag = kAlertViewTwo; 

y luego se diferencian entre ellos en los métodos de delegado mediante las siguientes etiquetas:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if(alertView.tag == kAlertViewOne) { 
     // ... 
    } else if(alertView.tag == kAlertViewTwo) { 
     // ... 
    } 
} 
+3

Ah , bonito. Aunque usaría un interruptor. :) – quano

+1

por supuesto, un interruptor funcionaría también, pero nunca me gustó el interruptor. =) –

+0

Gran respuesta. Pero eso me lleva a otra pregunta. ¿Por qué define kAlertViewOne 1 y kAlertViewRwo 2. ¿No podría usar simplemente en alertView.tag = 1 o alertView.tag = 2? ¿Esto se hace por alguna razón? – George

4

FYI, si desea orientar solo IOS 4 usuarios (que es razonable now that ~98.5% of clients have at least iOS 4 installed), debería poder usar Blocks para hacer un buen manejo en línea de UIAlertViews.

He aquí una pregunta Stackoverflow explicarla:
Block for UIAlertViewDelegate

He intentado utilizar BlocksKit marco de Zachary Waldowski para esto. Su referencia UIAlertView(BlocksKit) API se veía realmente bien. Sin embargo, traté de seguir his instructions to import the BlocksKit framework en mi proyecto, pero desafortunadamente no pude hacerlo funcionar.

Así que, como sugiere Can Berk Güder, he usado etiquetas UIAlertView por el momento. Pero en algún momento en el futuro voy a tratar de pasar a usar Blocks (preferiblemente uno que soporte ARC fuera de la caja).

+0

Si usa iOS 8+, UIAlertController tiene métodos de delegado basados ​​en bloques. –

1

Puede superar este suplicio y evitar el uso de etiquetas mejorando UIAlertView para utilizar devoluciones de llamadas en bloque. Echa un vistazo this blog post Escribí sobre el tema.

3

más fácil & nueva

UIAlertView *alert = [[UIAlertView alloc] init... 
alert.tag = 1; 

UIAlertView *alert = [[UIAlertView alloc] init... 
alert.tag = 2; 



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if(alertView.tag == 1) { 
     // first alert... 
    } else { 
     // sec alert... 
    } 
} 

todo hecho!

+0

Perfecto, gracias! –

0

Siempre he pensado que usar etiquetas es un poco complicado. Si los usa, al menos establezca algunas constantes definidas para los números de etiqueta.

En su lugar, utilizar las propiedades de esta manera:

En la sección de interfaz:

@property (nonatomic, weak) UIAlertView *overDueAlertView; 
@property (nonatomic, weak) UIAlertView *retryPromptAlertView; 

Creación de la vista de alertas:

UIAlertView *alert = [[UIAlertView alloc] init... 
self.overDueAlertView = alert; 
[alert show]; 

método Delegado:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (alertView == self.overDueAlertView) { 
    // Overdue alert 
    } else if (alertView == self.retryPromptAlertView) { 
    // Retry alert 
    } 
Cuestiones relacionadas