Pregunta

Estoy harto de escritura básica de UIAlertView, es decir:

UIAlertView *alert = [[UIAlertView alloc] initWith...]] //etc

En lugar de hacer esto, es posible poner todo esto en una función de "ayudante", donde puedo devolver el buttonIndex, o lo que sea una alerta por lo general regresa?

Para una función simple ayudante supongo que se podría alimentar a los parámetros por el título, el mensaje, no estoy seguro de si se puede pasar delegados en un parámetro sin embargo, o información paquete.

En pseudo-código, podría ser así:

someValueOrObject = Print_Alert(Title="", Message="", Delegate="", Bundle="") // etc

Cualquier ayuda en esto sería grande.

Gracias

¿Fue útil?

Solución

Esto es lo que escribí, cuando me enfermé de hacer lo mismo:

-(void)alert:(NSString *)title withBody:(NSString *)message firstButtonNamed:(NSString *)firstButtonName {
  [self alert: title withBody: message firstButtonNamed: firstButtonName withExtraButtons: nil informing: nil];
}

-(void)alert:(NSString *)title withBody:(NSString *)message firstButtonNamed:(NSString *)firstButtonName informing:(id)delegate {
  [self alert: title withBody: message firstButtonNamed: firstButtonName withExtraButtons: nil informing: delegate];
}

-(void)alert:(NSString *)title withBody:(NSString *)message firstButtonNamed:(NSString *)firstButtonName withExtraButtons:(NSArray *)otherButtonTitles informing:(id)delegate {
  UIAlertView *alert = [[UIAlertView alloc]
              initWithTitle: title
              message: message
              delegate: delegate
              cancelButtonTitle: firstButtonName
              otherButtonTitles: nil];
  if (otherButtonTitles != nil) {  
    for (int i = 0; i < [otherButtonTitles count]; i++) {
      [alert addButtonWithTitle: (NSString *)[otherButtonTitles objectAtIndex: i]];
    }
  }
  [alert show];
  [alert release];
}

No se puede escribir una función que mostrará una alerta y luego devolver un valor como un buttonIndex embargo, debido a que el valor de regresar sólo se produce cuando el usuario presiona un botón y el delegado hace algo.

En otras palabras, el proceso de hacer una pregunta con la UIAlertView es uno asíncrono.

Otros consejos

En 4.0+ puede simplificar el código de alerta utilizando bloques, un poco como esto:

CCAlertView *alert = [[CCAlertView alloc]
    initWithTitle:@"Test Alert"
    message:@"See if the thing works."];
[alert addButtonWithTitle:@"Foo" block:^{ NSLog(@"Foo"); }];
[alert addButtonWithTitle:@"Bar" block:^{ NSLog(@"Bar"); }];
[alert addButtonWithTitle:@"Cancel" block:NULL];
[alert show];

Alerta Lambda en GitHub .

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