Can I change UITextInputTraits while viewing a UIKeyboard?

I want to change the keyboard UITextInputTraits during use.

My ideal code would look something like this:

- (IBAction)nameTextDidChange:(UITextField *)sender { if ([sender.text isEqualToString:@""]) { sender.returnKeyType = UIReturnKeyDone; } else { sender.returnKeyType = UIReturnKeySearch; } } 

So ... I have another "Return" button for an empty line, as I am making a line with some text. The code I wrote above does not work, the keyboard retains the original features of text input.

Any ideas, or it will never work, no matter how hard I try?

Hooray!

Nick.

Thanks to Deepak, this is the code I really used:

 if ([sender.text isEqualToString:@""]) { sender.returnKeyType = UIReturnKeyDone; [sender resignFirstResponder]; [sender becomeFirstResponder]; } else if (sender.returnKeyType == UIReturnKeyDone) { NSString *cachedLetter = sender.text; sender.returnKeyType = UIReturnKeySearch; [sender resignFirstResponder]; [sender becomeFirstResponder]; sender.text = cachedLetter; } 
+6
iphone keyboard textinput
source share
3 answers

You can do this work by adding the following lines at the end of the method.

 if ([textField.text isEqualToString:@""]) { textField.returnKeyType = UIReturnKeyDone; [textField resignFirstResponder]; [textField becomeFirstResponder]; } else if (textField.returnKeyType == UIReturnKeyDone) { textField.returnKeyType = UIReturnKeySearch; [textField resignFirstResponder]; [textField becomeFirstResponder]; } 

That should work.

Basically you switch it on and off so that the text changes. Second, if you need to make sure that you flip only if necessary.

+5
source share

[textField reloadInputViews] seems to do the trick ...

+4
source share

resignFirstResponder / getFirstResponder and reloadInputViews have some caveats. See Stackoverflow.com/questions/5958427 / .... They change the keyboard back to its original state. Therefore, if the user switched to one of the other keyboard layouts (numbers, punctuation marks, etc.), this state will be lost. I have never found a good solution.

0
source share

All Articles