ios - 为什么这个委托(delegate)不触发是个谜

标签 ios delegates uialertview

我开发了这个类,它基本上是一个 UIAlertView,带有一个在 block 上运行的输入字段。因此,无论用户在 alertView 上做什么(单击取消、确定或填写文本),都会返回到一个 block :

标题

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

实现

@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

我运行它,它显示带有消息、占位符的 alertView,非常完美。我单击取消或填写文本并按确定,并且永远不会触发 alertview:clickedButtonAtIndex:。我不明白为什么。

谢谢

最佳答案

您可以直接继承 UIAlertView 并添加您自己的初始化程序。我已将您的类抽象为 UIAlertView 的单个子类,并且工作正常。请在下面查看我的帖子,

typedef void (^onClickBlock)(NSInteger buttonIndex, NSString *textoInput);

@interface AlertComBloco : UIAlertView<UIAlertViewDelegate>
@property (nonatomic, copy) onClickBlock runOnClickBlock;
@end


@implementation AlertComBloco


- (id)initWithTitulo:(NSString *)titulo mensagem:(NSString*)mensagem tituloBotaoCancel:(NSString*)tituloCancel outrosBotoesArray:(NSArray *)titulosOutrosBotoes inputComPlaceHolder:(NSString *)textoPlaceholder
    comBlocoExecucao:(onClickBlock)bloco{
  if(self = [self initWithTitle:titulo message:mensagem delegate:self cancelButtonTitle:tituloCancel otherButtonTitles:nil, nil]){
    _runOnClickBlock = bloco;
    for (NSString *umOutroBotao in titulosOutrosBotoes) {
      [self addButtonWithTitle:umOutroBotao];
    }
    [self setAlertViewStyle:UIAlertViewStylePlainTextInput];
    UITextField *input = [self textFieldAtIndex:0];
    input.placeholder = textoPlaceholder;

  }
  return self;
}



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

  UITextField *input = [alertView textFieldAtIndex:0];

  if (self.runOnClickBlock) {
    self.runOnClickBlock(buttonIndex, input.text);
  }

}

@end

然后,您可以像这样在 View Controller 中使用 block 调用它,

- (void)showAlert:(id)sender{
  AlertComBloco *alertCombo = [[AlertComBloco alloc] initWithTitulo:@"Hi" mensagem:@"Custom Alert" tituloBotaoCancel:@"Cancel" outrosBotoesArray:@[@"Other"] inputComPlaceHolder:@"Placeholder" comBlocoExecucao:^(NSInteger buttonIndex, NSString *textoInput) {
    NSLog(@"Button at index %ld, with text %@", (long)buttonIndex, textoInput);
  }];
  [alertCombo show];
}

关于ios - 为什么这个委托(delegate)不触发是个谜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26328139/

相关文章:

ios - 使用 NSMutableData iOS 进行键值观察

ios - UICollectionView/FlowLayout

ios - 导出的慢动作视频有音频问题

ios - 自定义 UIAlertView 没有委托(delegate)回调?

iphone - 如何从结果集中获取分组查询数据?

javascript - 在 subview 中获取 Backbone 父 View 事件

ios - 将 App Delegate 从 Objective-C 换成 Swift?

c# - 修复委托(delegate)的 'method group' 问题

android - Cocos2d-x 中的警报 View 等效项

ios - 在具有透明背景的 Alert ViewController 中心添加图像