我有4個uitextfields,可以識別它們何時爲空或完整。如果完成,他們將返回鍵類型更改爲GO,否則爲默認值。問題是,即使我使用reloadinputview編輯時更改uitextfield返回鍵類型
- (void)viewDidLoad
{
[super viewDidLoad];
_fieldsArray = @[_nameField, _passwordField, _emailField, _usernameField];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSRange textFieldRange = NSMakeRange(0, [textField.text length]);
//NSLog(@"%d", !(NSEqualRanges(range, textFieldRange) && [string length] == 0));
[self signUpFieldsAreValid:(!(NSEqualRanges(range, textFieldRange) && [string length] == 0) && [self validateSignUpFields:textField])];
[textField reloadInputViews];
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
[self signUpFieldsAreValid:NO];
[textField reloadInputViews];
return YES;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
for (UITextField *aTextField in _fieldsArray) {
if (aTextField.isFirstResponder) {
aTextField.layer.borderWidth = 0.f;
aTextField.layer.borderColor = nil;
}
}
textField.layer.borderWidth = 1.f;
textField.layer.borderColor = [UIColor colorWithRed:200.f/255.f green:0.f/255.f blue:4.f/255.f alpha:1.f].CGColor;
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSUInteger fieldIndex = [_fieldsArray indexOfObject:textField];
[_fieldsArray[(fieldIndex + 1) % 4] becomeFirstResponder];
return YES;
}
- (BOOL)validateSignUpFields:(UITextField *)firstResponder
{
for (UITextField *aTextField in _fieldsArray) {
if (!aTextField.text.length && ![aTextField isEqual:firstResponder]) {
return NO;
}
}
return YES;
}
- (void)signUpFieldsAreValid:(BOOL)valid
{
NSLog(@"%d", valid);
for (UITextField *aTextField in _fieldsArray) {
if (valid) {
aTextField.returnKeyType = UIReturnKeyGo;
}
else {
aTextField.returnKeyType = UIReturnKeyDefault;
}
}
}
試了一下,但現在它似乎沒有這個作用。是否有可能是因爲我使用ios 7會對其產生任何影響? –
不要在ios6和7上工作。並且會有一些副作用,因爲它會調用'textFieldDidEndEditing:'然後'textFieldDidBeginEditing:'方法。 – Martin
[textField resignFirstResponder]; [textField becomeFirstResponder];效果很好! –