gpt4 book ai didi

ios - 如何将选择器方法设置为 Objective-C 中文本更改的文本字段?

转载 作者:行者123 更新时间:2023-11-28 21:56:21 24 4
gpt4 key购买 nike

我正在为我的应用编写注册 View Controller 。我需要验证表格。我想到为文本值更改设置选择器方法应该适用于包含表单数据的不同文本字段。我在谷歌上看到了旧问题和东西,这就是我目前所知道的

- (void)viewDidLoad
{
[super viewDidLoad];
self.passwordInput.secureTextEntry = YES;
self.btnDone.enabled = NO; //Done button needs to be disabled until form is properly validated
self.emailInput.delegate = self; //emailinput is the property attached to Email textfield of the form
self.passwordInput.delegate = self;
emailCheck = NO;
passwordCheck = NO;
[self.emailInput addTarget:self action:@selector(formValidation) forControlEvents:UIControlEventValueChanged];
[self.passwordInput addTarget:self action:@selector(formValidation) forControlEvents:UIControlEventValueChanged];
// Do any additional setup after loading the view from its nib.
}

-(void) formValidation {

NSString *regex = @"[^@]+@[A-Za-z0-9.-]+\\.[A-Za-z]+";
NSPredicate *emailPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if(self.passwordInput.text.length >7)
{
passwordCheck = YES;
}
if([emailPredicate evaluateWithObject:self.emailInput.text])
{
emailCheck = YES;
}
if (self.passwordInput.text.length<7 || ![emailPredicate evaluateWithObject:self.emailInput])
{
self.warningLabel.text = @"Please enter a valid email/at least 8 character password";
}
if(passwordCheck == YES && emailCheck ==YES)
{
self.btnDone.enabled = YES;//button is enabled
} }

现在的问题是事件没有触发。输入数据时没有任何反应。谁能指导我这里做错了什么?
附言我不太明白UITextFieldTextDidChangeNotification .如果有人可以提出替代解决方案或为我解释该概念,那就太棒了我刚试过forControlEvents:UIControlEventEditingChanged ,应用程序崩溃并出现无法执行正则表达式的错误。 **"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't do regex matching on object <UITextField: 0x9ae8610;"**

最佳答案

设置textfields的delegate,使用下面的方法

-(void)textFieldDidEndEditing:(UITextField *)textField
{
[self validateForm:textField.text];
}

并将表单验证器函数更改为

-(void) validateForm:(NSString *)inputString {

//validate inputString

}

关于ios - 如何将选择器方法设置为 Objective-C 中文本更改的文本字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26382508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com