我有一个带有textField的UIAlertView和两个按钮:Save&取消。点击“保存”按钮时,我正在检查文本字段是否为空,如果是,我只想将textFields占位符更改为:@“请输入名称”并在屏幕上保留警报视图。但它会自动被驳回。
如何覆盖?
答案 0 :(得分:6)
将目标添加到子类alertView中的文本字段。您可以继承alertView,而不是解除as described in this post
[[alertView textFieldAtIndex:0] addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];
然后编写一个名为textFieldDidChange的函数,该函数检查alertView的当前文本字段并设置一个布尔值,以便您知道是否关闭警报。
- (void) textFieldDidChange
{
NSString *alertViewText = [[alertView textFieldAtIndex:0] text];
if ([alertViewText isEqualToString:@""]) {
[alertView setMessage:@"Enter a name please."];
} else {
[alertView setMessage:@"Default Message"];
}
}
*或者,我建议在它为空时禁用“保存”而不必进行子类化。 * 强>
- (void) textFieldDidChange
{
NSString *alertViewText = [[alertView textFieldAtIndex:0] text];
if ([alertViewText isEqualToString:@""]) {
[alertView setMessage:@"Enter a name please."];
for (UIViewController *view in alertView.subview) {
if ([view isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)view;
if ([[[button titleLabel] text] isEqualToString:@"Save"])
[button setEnabled:NO];
}
}
} else {
[alertView setMessage:@"Default Message"];
for (UIViewController *view in alertView.subview) {
if ([view isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)view;
if ([[[button titleLabel] text] isEqualToString:@"Save"])
[button setEnabled:YES];
}
}
}
}