gpt4 book ai didi

ios - 从委托(delegate)中检查 UITextField.text?

转载 作者:行者123 更新时间:2023-11-29 12:41:28 25 4
gpt4 key购买 nike

在我的申请中,我有 3 UITextField在我的标题中声明:

@property (strong, nonatomic) IBOutlet UITextField *email;
@property (strong, nonatomic) IBOutlet UITextField *username;
@property (strong, nonatomic) IBOutlet UITextField *password;

获得这些值后,我将它们分配给我的变量并继续其他帐户创建过程。

我正在尝试做的是以下,截至目前,我的 UITextField使用委托(delegate)方法中声明的 .tag 属性相互“制表”,如下所示:

-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
NSInteger nextTag = textField.tag + 1;

UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder) {

[nextResponder becomeFirstResponder];
} else {

[textField resignFirstResponder];
}
return NO;
}

我还有一个 alertView包含一个标签,提醒用户注意以下事项:

->用户名已存在,邮箱已存在,有效密码必须至少包含5个字符等。

我不想在点击“注册”按钮时提醒用户他们犯了上述任何错误,我希望他们在文本字段退出 firstResponder 并在至少 1 个字符是 .text属性(property)。

谁能给我指出正确的方向,谢谢!

更新

我已尝试实现 textFieldDidEndEditing委托(delegate)方法:

-(BOOL)textFieldDidEndEditing:(UITextField*)textField {


if (self.passwordTF.text.length <=4)
{
NSString *noValidPW = @"Passwords must be atleast 5 characters";
NSMutableAttributedString *noValidPWAttrString = [[NSMutableAttributedString alloc] initWithString:noValidPW attributes:alertAttrs];
self.alertLabel.attributedText = noValidPWAttrString;

[UIView transitionWithView:[self redAlert]
duration:0.2
options:UIViewAnimationOptionTransitionCrossDissolve
animations:NULL
completion:NULL];

self.redAlert.hidden = NO;

}

return NO;
}

但这不起作用,一旦我停止编辑任何字段,而不仅仅是密码字段,就会给我密码提醒。

更新这是生成动画警报的代码:

NSString *noValidPW = @"Passwords must be atleast 5 characters";
NSMutableAttributedString *noValidPWAttrString = [[NSMutableAttributedString alloc] initWithString:noValidPW attributes:alertAttrs];
self.alertLabel.attributedText = noValidPWAttrString;

[UIView transitionWithView:[self redAlert]
duration:0.2
options:UIViewAnimationOptionTransitionCrossDissolve
animations:NULL
completion:NULL];

self.redAlert.hidden = NO;

我对其他错误类型(对于其他文本字段)有类似的警报,因此能够检查哪个文本字段是刚刚结束编辑的文本字段很重要,它的 .text 上是否有任何字符?属性(property)?如果是这样,它需要通过我在上面提供的验证码。

最佳答案

您可以为此使用 -textFieldDidEndEditing:。您缺少的部分是确保调用委托(delegate)方法的 textField 是您感兴趣的那个。

例如:

- (void)textFieldDidEndEditing:(UITextField *)textField
{
// Check only if the user didn't leave the textfield blank
if ([textField.text length] >= 1)
{
if (textField == self.password)
{
NSString *passwordText = textField.text;
if ([passwordText length] < 5)
{
[self showPasswordLengthError];
}
else if ( /* check other password stuff */ )
{
[self showPasswordOtherError];
}
}
else if (textField == self.username)
{
// Check username conditions and show errors accordingly
}
else if (textField == self.email)
{
// Check email stuff
}
// etc.
}
}

- (void)showPasswordLengthError
{
NSString *noValidPW = @"Passwords must be at least 5 characters";
NSMutableAttributedString *noValidPWAttrString = [[NSMutableAttributedString alloc] initWithString:noValidPW attributes:alertAttrs];
self.alertLabel.attributedText = noValidPWAttrString;

[UIView transitionWithView:[self redAlert]
duration:0.2
options:UIViewAnimationOptionTransitionCrossDissolve
animations:NULL
completion:NULL];

self.redAlert.hidden = NO;

// Hide alertLabel after 5 sec
[self performSelector:@selector(hideAlertLabel) withObject:nil afterDelay:5.0];
}

- (void)hideAlertLabel
{
self.redAlert.hidden = YES;
}

关于ios - 从委托(delegate)中检查 UITextField.text?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24769145/

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