Tôi có 4 uitextfields nhận ra khi chúng trống hoặc hoàn thành. Nếu hoàn thành, họ thay đổi kiểu khóa trả về của họ thành GO, khác là kiểu mặc định. Vấn đề là bàn phím không thay đổi kiểu chìa khóa mặc dù tôi sử dụng các reloadinputviewThay đổi loại khóa trả về uitextfield trong khi chỉnh sửa
- (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;
}
}
}
thử nó ngay bây giờ nhưng nó không xuất hiện để có hiệu lực thi hành đó. Có thể là bởi vì tôi đang sử dụng ios 7 có bất kỳ loại hậu quả nào không? –
Không hoạt động trên ios6 & 7. Và sẽ có một số tác dụng phụ, vì nó sẽ gọi phương thức 'textFieldDidEndEditing:' rồi 'textFieldDidBeginEditing:'. – Martin
[textField resignFirstResponder]; [textField trở thànhFirstResponder]; Hoạt động tốt! –