我开发了这个类,它基本上是一个 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/