UITextField keyboard return button should change as Done button
-
02-07-2021 - |
Question
I am having five number of textFields, say textField1,textField2,textField3,textField4,textField5.
What i want to do is that the UITextField
keyboard return button should change as Done button when all the textField contains some text.
I have tried,
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([textField1.text doubleValue]!=0)
if([textField2.text doubleValue]!=0)
if([textField3 doubleValue]!=0)
if([textField4 doubleValue]!=0)
if([textField5 doubleValue]!=0)
{
[textField setReturnKeyType:UIReturnKeyDone];
}
else
[textField setReturnKeyType:UIReturnKeyDefault];
else
[textField setReturnKeyType:UIReturnKeyDefault];
else
[textField setReturnKeyType:UIReturnKeyDefault];
else
[textField setReturnKeyType:UIReturnKeyDefault];
else
[textField setReturnKeyType:UIReturnKeyDefault];
}
I also tried in
- (void)textFieldDidEndEditing:(UITextField *)textField
{
}
Both are not working... Please anyone help me...
Solution
you better play with textFieldShouldBeginEditing, here just check all the textfields are contains non null values,
so code should look like this
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if([[tf1.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] > 0 && [[tf2.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] > 0 [[tf3.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] > 0) {
[textField setReturnKeyType:UIReturnKeyDone];
}
return TRUE;
}
//// EDIT
I've checked this code. It works fine. so try this and lemme know.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if ([self areAllFieldsNonEmpty]) {
[textField setReturnKeyType:UIReturnKeyDone];
[textField reloadInputViews];
} else {
[textField setReturnKeyType:UIReturnKeyDefault];
[textField reloadInputViews];
}
return TRUE;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([self areOtherFiledsNonEmptyThan:textField]) {
if (range.location == 0 && range.length == 0) {
[textField setReturnKeyType:UIReturnKeyDone];
[textField reloadInputViews];
} else if (range.location == 0 && range.length == 1) {
[textField setReturnKeyType:UIReturnKeyDefault];
[textField reloadInputViews];
}
}
return TRUE;
}
- (NSInteger)lengthOf:(UITextField *)tf {
return [[tf.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length];
}
- (BOOL)areOtherFiledsNonEmptyThan:(UITextField *)tf {
BOOL isEmpty = TRUE;
for (UITextField *textField in self.view.subviews) {
if ([textField isKindOfClass:[UITextField class]] && tf != textField) {
if ([self lengthOf:textField] == 0) {
isEmpty = FALSE;
break;
}
}
}
return isEmpty;
}
- (BOOL)areAllFieldsNonEmpty {
BOOL isEmpty = TRUE;
for (UITextField *textField in self.view.subviews) {
if ([textField isKindOfClass:[UITextField class]]) {
if ([self lengthOf:textField] == 0) {
isEmpty = FALSE;
break;
}
}
}
return isEmpty;
}
OTHER TIPS
try this
if([your_textField.text length]>0)
your_textField.returnKeyType = UIReturnKeyDone;
You will have to check your condition in two methods:
-(void)textFieldDidBeginEditing:(UITextField *)textField;
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
So make a function for condition for reusability of code -
-(void)checkCondition;
And then do this in your ViewController.m
class -
-(void)checkCondition
{
if([txtField1.text length]>0 && [txtField2.text length]>0 && [txtField3.text length]>0 && [txtField4.text length]>0 && [txtField5.text length]>0)
{
txtField1.returnKeyType = UIReturnKeyDone;
txtField2.returnKeyType = UIReturnKeyDone;
txtField3.returnKeyType = UIReturnKeyDone;
txtField4.returnKeyType = UIReturnKeyDone;
txtField5.returnKeyType = UIReturnKeyDone;
}
else
{
txtField1.returnKeyType = UIReturnKeyDefault;
txtField2.returnKeyType = UIReturnKeyDefault;
txtField3.returnKeyType = UIReturnKeyDefault;
txtField4.returnKeyType = UIReturnKeyDefault;
txtField5.returnKeyType = UIReturnKeyDefault;
}
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[self checkCondition];
[textField reloadInputViews];
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
{
[self checkCondition];
[textField reloadInputViews];
}
Remember space
is count as a character
, So if you dont want to count spaces
then first remove them from textField.text
and then compare.