仅在input后启用UIAlertController的UIAlertAction
我正在使用UIAlertController
来显示一个对话框,其中包含一个UITextField
和一个标有“Ok”的UIAlertAction
button。 如何禁用该button,直到input到UITextField
的字符(如5个字符)为止?
在头文件中添加以下属性
@property(nonatomic, strong)UIAlertAction *okAction;
然后将下面的代码复制到ViewController
viewDidLoad
方法中
self.okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]; self.okAction.enabled = NO; UIAlertController *controller = [UIAlertController alertControllerWithTitle:nil message:@"Enter your text" preferredStyle:UIAlertControllerStyleAlert]; [controller addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.delegate = self; }]; [controller addAction:self.okAction]; [self presentViewController:controller animated:YES completion:nil];
还要在您的类中实现以下UITextField
委托方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ NSString *finalString = [textField.text stringByReplacingCharactersInRange:range withString:string]; [self.okAction setEnabled:(finalString.length >= 5)]; return YES; }
这应该工作
你可以添加一个观察者到你的UITextField
:
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { [textField addTarget:self action:@selector(alertControllerTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; }
但首先禁用您的button:
okAction.enabled = NO;
然后用你指定的方法validation它:
- (void)alertTextFieldDidChange:(UITextField *)sender { UIAlertController *alertController = (UIAlertController *)self.presentedViewController; if (alertController) { UITextField *someTextField = alertController.textFields.firstObject; UIAlertAction *okAction = alertController.actions.lastObject; okAction.enabled = someTextField.text.length > 2; } }
Swift 3的实现基于灵魂的回答:
var someAlert: UIAlertController { let alert = UIAlertController(title: "Some Alert", message: nil, preferredStyle: .alert) alert.addTextField { $0.placeholder = "Write something" $0.addTarget(self, action: #selector(self.textFieldTextDidChange(_:)), for: .editingChanged) } alert.addAction(UIAlertAction(title: "Cancel", style: .cancel)) let submitAction = UIAlertAction(title: "Submit", style: .default) { _ in // Do something... } submitAction.isEnabled = false alert.addAction(submitAction) return alert } func textFieldTextDidChange(_ textField: UITextField) { if let alert = presentedViewController as? UIAlertController, let action = alert.actions.last, let text = textField.text { action.isEnabled = text.characters.count > 0 } }
更好的方法是在validation他的input之后提醒用户他的input有什么问题,以便用户知道应用程序对他的期望。
- (void)askReasonWithPreviousReason:(NSString *)text { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert" message:@"Enter reason" preferredStyle:UIAlertControllerStyleAlert]; [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.text = text; }]; [alertController addAction:[UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { if ([self isReasonValid:alertController.textFields.firstObject.text]) { UIAlertController *alertController2 = [UIAlertController alertControllerWithTitle:AlertTitle message:@"Are you sure you would like to save?" preferredStyle:UIAlertControllerStyleAlert]; [alertController2 addAction:[UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self saveReason:alertController.textFields.firstObject.text]; }]]; [alertController2 addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:Nil]]; [self presentViewController:alertController2 animated:YES completion:nil]; } }]]; [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:Nil]]; [self presentViewController:alertController animated:YES completion:nil]; } - (BOOL)isReasonValid:(NSString *)reason { NSString *errorMessage = [[NSString alloc] init]; if (reason.length < 5) { errorMessage = @"Reason must be more than 5 characters"; } else if (reason.length > 100) { errorMessage = @"Reason must be less than 100 characters"; } if (errorMessage.length != 0) { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert" message:errorMessage preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self askReasonWithPreviousReason:reason]; }]]; [self presentViewController:alertController animated:YES completion:nil]; return NO; } return YES; }
我有一个问题,在stackoverflow基本上问同一个问题的答案 。 总结一下,有几种方法可以做到这一点:使用UITextFieldDelegate,Notification,KVO,或者直接在控件上添加事件处理目标。 我的解决scheme是一个简单的UIAlertController子类,包裹在事件处理目标周围,你可以简单地通过调用来configuration
alert.addTextField(configurationHandler: { (textField) in textField.placeholder = "Your name" textField.autocapitalizationType = .words }) { (textField) in saveAction.isEnabled = (textField.text?.characters.count ?? 0) > 0 }
如果您不得不在项目中多次处理此类警报,这应该很方便。