forum

Home / DeveloperSection / Forums / delegate not triggering is a mystery in ios

delegate not triggering is a mystery in ios

Anonymous User194013-Oct-2014

I have developed this class that is basically a UIAlertView with an input field that runs on blocks. So, whatever the user does on the alertView (click cancel, ok, or fill the text), returns to a block:

HEADER

typedef void (^onClickBlock)(NSInteger buttonIndex, NSString *textoInput);
 
 
@interface AlertViewBlocoComInput : NSObject
 
- (void)mostrarAlertViewBlocoComTitulo:(NSString *)titulo
                              mensagem:(NSString *)mensagem
                     tituloBotaoCancel:(NSString *)tituloCancel
                     outrosBotoesArray:(NSArray *)titulosOutrosBotoes
                   inputComPlaceHolder:(NSString *)textoPlaceholder
                      comBlocoExecucao:(onClickBlock)bloco;
 
@end


IMPLEMENTATION

 

@interface AlertComBloco : UIAlertView
@property (nonatomic, copy) onClickBlock runOnClickBlock;
@end
 
 
@implementation AlertComBloco
@end
 
 
@interface AlertViewBlocoComInput () <UIAlertViewDelegate>
@end
 
 
@implementation AlertViewBlocoComInput
 
 
- (void)mostrarAlertViewBlocoComTitulo:(NSString *)titulo
                              mensagem:(NSString *)mensagem
                     tituloBotaoCancel:(NSString *)tituloCancel
                     outrosBotoesArray:(NSArray *)titulosOutrosBotoes
                   inputComPlaceHolder:(NSString *)textoPlaceholder
                      comBlocoExecucao:(onClickBlock)bloco
 
{
 
  AlertComBloco *alerta = [[AlertComBloco alloc] initWithTitle:titulo
                                                       message:mensagem
                                                      delegate:self
                                             cancelButtonTitle:tituloCancel
                                             otherButtonTitles:nil];
  alerta.runOnClickBlock =bloco;
 
  // adicionar os outros botões
  for (NSString *umOutroBotao in titulosOutrosBotoes) {
    [alerta addButtonWithTitle:umOutroBotao];
  }
 
 
  [alerta setAlertViewStyle:UIAlertViewStylePlainTextInput];
  UITextField *input = [alerta textFieldAtIndex:0];
  input.placeholder = textoPlaceholder;
 
  [alerta show];
 
}
 
 
 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
 
  AlertComBloco *alerta = (AlertComBloco *)alertView;
  onClickBlock bloco = alerta.runOnClickBlock;
 
  UITextField *input = [alertView textFieldAtIndex:0];
 
  if (bloco) {
    bloco(buttonIndex, input.text);
  }
 
}
 
 
@end




run it, it shows the alertView with the message, placeholder, perfect. I click cancel or fill the text and press ok and the alertview:clickedButtonAtIndex: is never triggered. I am not seeing why.


Updated on 13-Oct-2014
I am a content writter !

Can you answer this question?


Answer

2 Answers

Liked By