Reputation: 492
I tried - How to disable/enable the return key in a UITextField? but this gives compilation error.
Requirement : Return key should be disabled until user enters 9 characters.
I tried textfield.enablesReturnKeyAutomatically = YES;
Using this return key was disabled when no input text is available in text field.As soon as i enter text its becoming enable.
Is there any solution which works for this?
Upvotes: 1
Views: 3166
Reputation: 349
You cannot disable returnKey with text in the UITextField
. As specified in Apple Doc
If you set it to YES, the keyboard disables the Return key when the text entry area contains no text.
For the behaviour you want to achieve, you can block the code that will be executed on click on returnKey
Conform your class to UITextFieldDelegate
Set the
textfield.delegate = self
Implement the protocol method
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if( textField.text.length < 9)
{
return NO;
}
return YES;
}
Upvotes: 1
Reputation: 625
please select "Auto Enable Return key"
Try this one its work for me
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if (newString.length == 9) {
self.txtTextField.enablesReturnKeyAutomatically = YES;
}
return (newString.length<=10);
}
Upvotes: 1
Reputation: 5823
You can use UITextFieldDelegate method to find 9 characters, and enable key that time until disable it. show the code below: I have written this code in swift you can write in Objective-C
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
// Limit to 9 characters
if range.location >= 9 {
txtCity.enablesReturnKeyAutomatically = true
} else {
txtCity.enablesReturnKeyAutomatically = false
}
Upvotes: 0