Using the keyboard -- setting target - action with Done button on the keyboard
I've been looking through documentation and sample code and it almost always uses an external button to dismiss the keyboard and return what's in the text. I want to know how you set the target-action on the "Done" or "Return" or whatever button on the keyboard so that when you're done typing, the keyboard disappears, the view changes, the text is returned etc.
How do you access that "Done" button?
I also tried
[userNameInput addTarget:self action:@selector(editingOver: ) forControlEvents:UIControlEventEditingDidEndOnExit];
but the control event doesn't seem to return unless you actually exit the text field...?
I think my main problem is setting the target - action method... I tried with a simple button, couldn't get it to work...
Most likely what you are seeing isn't being done to the keyboard directly. The other button is telling the text field to resignFirstResponder, and that's what makes the keyboard go away. This would be done in the IBAction that you assign to the button.
you can create a IBAction and in IB hook up to the textfield's DidEndOnExit event, and in the method just dismiss the keyboard (or "resign" it), and finish out with the rest of your method. I didn't run this but off the top of my head I feel like that's the proper way to do it
One should be able to do it with or without Interface Builder -- I mean its basically equibalent. Why isn't the setAction method working?
I just assumed you (OP?) would be fine using IB. I prefer doing (most) everything programmatic as well, I just saw this as the easiest way to do it. I think you meant "equivalent" by the way, I mention the typo because I bothered going on to dictionary.com to get no matches, before realizing it was a typo.
I just assumed you (OP?) would be fine using IB. I prefer doing (most) everything programmatic as well, I just saw this as the easiest way to do it. I think you meant "equivalent" by the way, I mention the typo because I bothered going on to dictionary.com to get no matches, before realizing it was a typo.
You should implement the textfield delegate method below, it is called when you click the Done button on the keyboard
Code:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder]; //dismiss the keyboard
//do whatever else you need with the text
While this works well, I might add that if you don't have the Delegate set to the First Responder (Top item "delegates" under Outlets on the Nib Connections Inspector view (Command-2)) then no matter what, it won't dismiss that silly keyboard! I struggled for hours with that, and used a "touch anywhere else on the screen" workaround before figuring that out.
Frustrating, to say the least. Boy, do I miss VB6!
You should implement the textfield delegate method below, it is called when you click the Done button on the keyboard
Code:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder]; //dismiss the keyboard
//do whatever else you need with the text
If your are using UITextView, and have header interface implementing <UITextViewDelegate> the answer is different than for a textfield. Often it is best to have a button above the keyboard to dismiss it, but you can trigger return from the DONE/return key:
Code:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
int charINTtyped=0;
if([text length]>0){
charINTtyped=(int)[text characterAtIndex:0];
}
if(charINTtyped==10){
[textView resignFirstResponder]; //dismiss the keyboard
return NO;//don't insert return into textView.text
}
return YES;
}