jon*_*gen 50 swift uialertcontroller
我增加了UITextField一个UIAlertController,这似乎为AlertView.在解雇之前UIAlertController,我想验证输入UITextField.基于验证,我想解雇UIAlertController或不解雇.但我不知道如何防止UIAlertController按下按钮时的解除动作.有没有人解决这个问题或任何想法从哪里开始?我去谷歌但没有运气:/谢谢!
mat*_*att 71
你是对的:如果用户可以点击警报中的按钮,则警报将被取消.所以你想阻止用户点击按钮!这只是禁用UIAlertAction按钮的问题.如果禁用警报操作,则用户无法点击它以解除警报.
要使用文本字段验证结合这一点,使用文本字段的委托方法或操作方法(在当你创建的文本字段的配置处理器配置)启用/禁用适当地取决于什么文本有(或没有)已经进入UIAlertActions .
这是一个例子.我们创建了这样的文本字段:
alert.addTextFieldWithConfigurationHandler {
(tf:UITextField!) in
tf.addTarget(self, action: "textChanged:", forControlEvents: .EditingChanged)
}
Run Code Online (Sandbox Code Playgroud)
我们有一个取消操作和一个OK操作,我们将OK操作带入了禁用的世界:
(alert.actions[1] as UIAlertAction).enabled = false
Run Code Online (Sandbox Code Playgroud)
随后,除非文本字段中有一些实际文本,否则用户无法点击OK:
func textChanged(sender:AnyObject) {
let tf = sender as UITextField
var resp : UIResponder = tf
while !(resp is UIAlertController) { resp = resp.nextResponder() }
let alert = resp as UIAlertController
(alert.actions[1] as UIAlertAction).enabled = (tf.text != "")
}
Run Code Online (Sandbox Code Playgroud)
编辑这是上面代码的当前(Swift 3.0.1及更高版本)版本:
alert.addTextField { tf in
tf.addTarget(self, action: #selector(self.textChanged), for: .editingChanged)
}
Run Code Online (Sandbox Code Playgroud)
和
alert.actions[1].isEnabled = false
Run Code Online (Sandbox Code Playgroud)
和
@objc func textChanged(_ sender: Any) {
let tf = sender as! UITextField
var resp : UIResponder! = tf
while !(resp is UIAlertController) { resp = resp.next }
let alert = resp as! UIAlertController
alert.actions[1].isEnabled = (tf.text != "")
}
Run Code Online (Sandbox Code Playgroud)
ull*_*trm 16
我已经简化了亚马的答案而没有观察层次遍历.这将把动作本身作为一个弱变量.这是一个完全有效的例子:
weak var actionToEnable : UIAlertAction?
func showAlert()
{
let titleStr = "title"
let messageStr = "message"
let alert = UIAlertController(title: titleStr, message: messageStr, preferredStyle: UIAlertControllerStyle.Alert)
let placeholderStr = "placeholder"
alert.addTextFieldWithConfigurationHandler({(textField: UITextField) in
textField.placeholder = placeholderStr
textField.addTarget(self, action: "textChanged:", forControlEvents: .EditingChanged)
})
let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { (_) -> Void in
})
let action = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: { (_) -> Void in
let textfield = alert.textFields!.first!
//Do what you want with the textfield!
})
alert.addAction(cancel)
alert.addAction(action)
self.actionToEnable = action
action.enabled = false
self.presentViewController(alert, animated: true, completion: nil)
}
func textChanged(sender:UITextField) {
self.actionToEnable?.enabled = (sender.text! == "Validation")
}
Run Code Online (Sandbox Code Playgroud)
关于@Matt的回答,这就是我在Obj-C中做同样事情的方式
- (BOOL)textField: (UITextField*) textField shouldChangeCharactersInRange: (NSRange) range replacementString: (NSString*)string
{
NSString *newString = [textField.text stringByReplacingCharactersInRange: range withString: string];
// check string length
NSInteger newLength = [newString length];
BOOL okToChange = (newLength <= 16); // don't allow names longer than this
if (okToChange)
{
// Find our Ok button
UIResponder *responder = textField;
Class uiacClass = [UIAlertController class];
while (![responder isKindOfClass: uiacClass])
{
responder = [responder nextResponder];
}
UIAlertController *alert = (UIAlertController*) responder;
UIAlertAction *okAction = [alert.actions objectAtIndex: 0];
// Dis/enable Ok button based on same-name
BOOL duplicateName = NO;
// <check for duplicates, here>
okAction.enabled = !duplicateName;
}
return (okToChange);
}
Run Code Online (Sandbox Code Playgroud)
我意识到这是在 Objectiv-C 中,但它显示了主体。稍后我将使用 swift 版本更新它。
您也可以使用块作为目标来执行相同的操作。
向您添加一个属性,ViewController以便块(swift 的闭包)具有强引用
@property (strong, nonatomic) id textValidationBlock;
然后AlertViewController像这样创建:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
__weak typeof(self) weakSelf = self;
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[weakSelf doSomething];
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[alertController.actions lastObject].enabled = NO;
self.textValidationBlock = [^{
UITextField *textField = [alertController.textFields firstObject];
if (something) {
alertController.message = @"Warning message";
[alertController.actions lastObject].enabled = NO;
} else if (somethingElse) {
alertController.message = @"Another warning message";
[alertController.actions lastObject].enabled = NO;
} else {
//Validation passed
alertController.message = @"";
[alertController.actions lastObject].enabled = YES;
}
} copy];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"placeholder here";
[textField addTarget:weakSelf.textValidationBlock action:@selector(invoke) forControlEvents:UIControlEventEditingChanged];
}];
[self presentViewController:alertController animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17014 次 |
| 最近记录: |