I’m trying to limit the amount of characters that a user can input. I have that working, the only problem is it’s now stopped the keyboard from closing. For example I want to restrict input to 3 characters, I type two characters and press done on the keyboard the keyboard closes but if I type 3 characters and press done the keyboard doesn’t close any ideas as to why?
heres my code
(void)viewDidLoad
{
NSLog(@"%@", self.chosenTime);
[self startGame];
[super viewDidLoad];
self.nameTextField.delegate = self;
NSLog(@"%@", self.playerName);
NSString *timeString = self.chosenTime;
self.timer = [timeString intValue];
self.timeSelected = [timeString intValue];
self.scoreTimer = 1000;
self.countdown.text = timeString;
// Do any additional setup after loading the view.
}
- (IBAction)hideKeyboard:(id)sender {
NSLog(@"Hello");
[self.nameTextField resignFirstResponder];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
self.playerName = [textField.text stringByReplacingCharactersInRange:range withString:string];
return !([self.playerName length] >= 4);
}
Try this, change the last two methods to these three: