在我的应用程序中,我在标题中声明了3 UITextField
:
@property (strong, nonatomic) IBOutlet UITextField *email;
@property (strong, nonatomic) IBOutlet UITextField *username;
@property (strong, nonatomic) IBOutlet UITextField *password;
获得值后,我将它们分配给我的变量并继续使用其他帐户创建过程。
我想要做的是以下内容,截至目前,我的UITextField
“Tab”彼此使用委托方法中声明的.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
,其中包含一个标签,可以提醒用户注意以下内容:
- >用户名已存在,Email已存在,有效密码必须至少包含5个字符等。
当点击“注册”按钮时,我不想提醒用户他们已经犯了上面提到的任何错误,我希望他们在文本字段重新启动firstResponder后立即知道他们的错误并且在其中至少有1个字符.text
属性。
有人可以指出我正确的方向,谢谢!
更新
我试图实现textFieldDidEndEditing
委托方法:
-(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
属性上是否有任何字符很重要?如果是这样,它需要传递我在此上面提供的验证码。
答案 0 :(得分:1)
您可以使用-textFieldDidEndEditing:
。您遗失的部分是确保调用委托方法的textField
是您感兴趣的方法。
E.g:
- (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;
}