xcode button uialertview ibaction

xcode - Vista UIAlert mĂșltiple; Cada uno con sus propios botones y acciones.



uialertcontroller swift 4 (5)

Hay una tag propiedad útil para UIView (desde la cual se subclase UIAlertView ). Puede establecer diferentes etiquetas para cada vista de alerta.

ACTUALIZAR:

#define TAG_DEV 1 #define TAG_DONATE 2 - (IBAction)altdev { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"titleGoesHere" message:@"messageGoesHere" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Continue", nil]; alert.tag = TAG_DEV; [alert show]; } - (IBAction)donate { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"titleGoesHere" message:@"messageGoesHere" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Continue", nil]; alert.tag = TAG_DONATE; [alert show]; } -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (alertView.tag == TAG_DEV) { // handle the altdev ... } else if (alertView.tag == TAG_DONATE){ // handle the donate } }

Im creando una vista en Xcode 4.3 y no estoy seguro de cómo especificar múltiples UIAlertView que tienen sus propios botones con acciones separadas. Actualmente, mis alertas tienen sus propios botones, pero las mismas acciones. A continuación se muestra mi código.

-(IBAction)altdev { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"titleGoesHere" message:@"messageGoesHere" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Continue", nil]; [alert show]; } -(IBAction)donate { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"titleGoesHere" message:@"messageGoesHere" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Continue", nil]; [alert show]; } -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 1) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.examplesite1.com"]]; } } -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 1) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"examplesite2.com"]]; } }

¡Gracias por cualquier ayuda!


O puede hacer esto (verifique el nombre del título), es solo otra opción ... ¡Sin embargo, tenga en cuenta las alertas con el mismo nombre!

-(IBAction)altdev { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"titleOneGoesHere" message:@"messageGoesHere" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Continue", nil]; [alert show]; } -(IBAction)donate { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"titleTwoGoesHere" message:@"messageGoesHere" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Continue", nil]; [alert show]; } -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 1) { if([[alertView title] isEqualToString:@"titleOneGoesHere"]) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.examplesite1.com"]]; } else { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"examplesite2.com"]]; } }


Si le resulta difícil usar métodos delegados para identificar de manera diferente la vista de alerta, entonces también puede usar esta categoría de Categoría para usar el Bloque de finalización para cada AlertView.

Alert_ActionSheetWithBlocks

Por ejemplo.

UIAlertView* alert1 = [[UIAlertView alloc] initWithTitle:@"AlertView+Block 1" message:@"WithBlocks" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; [alert1 showWithFinishBlock:^(UIAlertView *alertView, NSInteger buttonIndex){ //--AlertView1 Stuff here }]; UIAlertView* alert2 = [[UIAlertView alloc] initWithTitle:@"AlertView+Block 2" message:@"WithBlocks" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; [alert2 showWithFinishBlock:^(UIAlertView *alertView, NSInteger buttonIndex){ //--AlertView2 Stuff here }];

Espero que esta sea la forma más fácil de comparar con el método de etiqueta + delegado.


Tiene razón, pero necesitas agregar esto:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (alertView.tag == TAG_DEV && buttonIndex == 1) { // handle the altdev ... } else if (alertView.tag == TAG_DONATE && buttonIndex == 1){ // handle the donate } }

si buttonIndex == 1 entonces está usando el PRIMER otro botón. 0 sería para cancelar. Pero no hagas nada por 0


más fácil y más nuevo

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 listo!