¿Alguien puede explicar cómo funciona el delegado en un UIAlertView
? ¿Se llama automáticamente o tengo que llamarlo? Por ejemplo:UIAlertView Delegados
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
¿Alguien puede explicar cómo funciona el delegado en un UIAlertView
? ¿Se llama automáticamente o tengo que llamarlo? Por ejemplo:UIAlertView Delegados
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
Mientras se está configurando correctamente la propiedad delegado del UIAlertView e implementar el protocolo, se llama automáticamente cuando un usuario hace clic en un botón en su alerta.
Eche un vistazo a los proyectos enumerados en "Código de muestra relacionado" en http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html para verlo en acción.
parece que no estaba llamando a delegado: auto en el init, gracias por la respuesta rápida – Joe
El método alertView:clickedButtonAtIndex:
del delegado se llama automáticamente por UIAlertView
. El método init para UIAlertView
toma un delegado como uno de los parámetros. Solo asegúrese de pasar un objeto que responda al alertView:clickedButtonAtIndex:
.
Digamos que mostró una alerta cuando el delegado fue "sí"
- (void)showAlert {
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"My Alert"
message:@"Do you want to continue?"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"No", @"Yes", nil];
[myAlert show];
[myAlert release];
}
Para que el siguiente para trabajar en su archivo .m:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
Su archivo .h necesitarán para hacer referencia al UIAlertViewDelegate en la instrucción de implementación de la siguiente manera:
@interface myViewController : UIViewController <UIAlertViewDelegate> {
}
Esto es lo que permite nuestro archivo .m para responder a las llamadas al método UIAlertViewDelegate.
Tener el delegado enumerado en el encabezado no es necesario, pero es una buena práctica – braden
¿Por qué esto no se dice en la documentación? https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html#//apple_ref/occ/instm/UIAlertView/initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles: –
Aquí hay un contenedor para el delegado para que pueda usar bloques en su lugar. El flujo de ejecución será el mismo, pero el flujo del código será más fácil de seguir. Por lo tanto, el uso de:
[YUYesNoListener yesNoWithTitle:@"My Title" message:@"My Message" yesBlock:^
{
NSLog(@"YES PRESSED!");
}
noBlock:^
{
NSLog(@"NO PRESSED!");
}];
... y aquí está la clase de ayuda:
typedef void(^EmptyBlockType)();
@interface YUYesNoListener : NSObject <UIAlertViewDelegate>
@property (nonatomic, retain) EmptyBlockType yesBlock;
@property (nonatomic, retain) EmptyBlockType noBlock;
+ (void) yesNoWithTitle:(NSString*)title message:(NSString*)message yesBlock:(EmptyBlockType)yesBlock noBlock:(EmptyBlockType)noBlock;
@end
@implementation YUYesNoListener
@synthesize yesBlock = _yesBlock;
@synthesize noBlock = _noBlock;
- (id) initWithYesBlock:(EmptyBlockType)yesBlock noBlock:(EmptyBlockType)noBlock
{
self = [super init];
if (self)
{
self.yesBlock = [[yesBlock copy] autorelease];
self.noBlock = [[noBlock copy] autorelease];
}
return self;
}
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0 && self.noBlock)
self.noBlock();
else if (buttonIndex == 1 && self.yesBlock)
self.yesBlock();
[_yesBlock release];
[_noBlock release];
[alertView release];
[self release];
}
- (void) alertViewCancel:(UIAlertView *)alertView
{
if (self.noBlock)
self.noBlock();
[_yesBlock release];
[_noBlock release];
[alertView release];
[self release];
}
+ (void) yesNoWithTitle:(NSString*)title message:(NSString*)message yesBlock:(EmptyBlockType)yesBlock noBlock:(EmptyBlockType)noBlock
{
YUYesNoListener* yesNoListener = [[YUYesNoListener alloc] initWithYesBlock:yesBlock noBlock:noBlock];
[[[UIAlertView alloc] initWithTitle:title message:message delegate:yesNoListener cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil] show];
}
@end
escribí una clase genérica para la sustitución de delegación UIAlertView con devoluciones de llamada de bloque. Puedes consultarlo [aquí] (http://stavash.wordpress.com/2013/01/31/quick-tip-uialertview-with-a-block-callback/). – Stavash