I am moving my view when a text field is pressed in order to get proper view when keyboard appears. Now, when the Done keyboard button is pressed, I would like to return the view to its initial state. How do I handle an action when the done keyboard button is pressed?
The proper way to do this is to observe to the notifications UIKeyboardDidShowNotification and UIKeyboardDidHideNotification as detailed in Apple's documentation.
If you want to know when the Done button has been pressed, implement
- (BOOL)textFieldShouldReturn:(UITextField *)textField
In your delegate. It should be called when the return button is pressed. See API documentation for more details.
Make an IBAction and connect it to the text field's didEndOnExit method. Then within the implementation of this method you should put [yourTextFieldOutlet resignFirstResponder];, which will deactivate the text field.
#freespace has it right, this is all you need to do.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
return [textField resignFirstResponder];
}
Tap the done button and poof, the keyboard is gone.
If you just want to know that Done was pressed, you can ask to be told of that control event:
[textField addTarget:self
action:#selector(donePressed)
forControlEvents:UIControlEventEditingDidEndOnExit];
That's the code version of IB's didEndOnExit.
Related
how can we add coustom buttons in defaultkeypad or how to write action for "go" or "done" button in keypad,,
thanks
for writing action fro "go" and "done" button you have to implement the correspondence delegate of your component, for example if you are using a UItextField you have to implement the following delegate -
-(BOOL) textFieldShouldReturn:(UITextField *)textField
Hook the UITextField's Did End On Exit action.
I have a form that looks like the following (see image). If the user hits the Login button, I want the keypad to disappear. How do I do that.
Note that TextFieldDelegate methods wouldnt get called since the user is simply hitting the UIButton (Login). Hence, anything I can put in the IBAction for this button?
Normally the keyboard should be dismissed automatically when the user taps somewhere outside of the textfield, but you can also manually hide it using
[textField resignFirstResponder]
set delegate for your text field <UITextFieldDelegate>
and over ride this method
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
I added a text field, and when I tap in there, the keyboard slides up. What are the steps needed that the keyboard shows an "OK" which actually really works? I mean...hitting "OK" or "Return" does exactly nothing.
Is there some sort of KeyboardDelegateProtocol where I must do some weird stuff like
-(BOOL)shouldReallyGoAway {
return YES;//of course!
}
Is there an 200 pages keyboard programming guide to read? Couldn't find one...
You need to handle the tap on the 'OK' button (Did End On Exit event) and resign the first responder status for the text field.
- (IBAction)textFieldDoneEditing:(id)sender {
[sender resignFirstResponder];
}
You might want to handle few other actions - like the background tap. You'll need an invisible button that will cover your background and trap the taps and an IBOutlet for your text field (since your sender will be the button, not the text field)
No reason for a 200 pages guide, it's right there in the introduction of the UITextField which you propably use.
Use
[aTextField resignFirstResponder];
to make the keyboard go away programmatically.
Also, there is the UITextFieldDelegate with appropriate methods regarding Return, which you might use, i.e. -textFieldShouldReturn:.
e.g.
- (BOOL) textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
I have a regular text field on a view and I'd like to make use of the search button on the iPhones keyboard. For the life of me, I can't figure out how to do this. There doesn't seem to be any event exposed that I can wire up that specifically relates to the search button on the keyboard. I've googled around, but I also haven't found anything related to this subject.
Make the keyboard display the blue "Search" button by setting the return key type.
myTextField.returnKeyType = UIReturnKeySearch;
Set the delegate of your text field to your controller and implement the 'textFieldShouldReturn:' method.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
/* Do some searching here */
return YES;
}
I want to open a panel when the user clicks on a text field. I think I should use a delegate method that responds to the click event. I found that the
- (void)textDidBeginEditing:(NSNotification *)aNotification
method does not work, and that the
- (void)controlTextDidBeginEditing:(NSNotification *)aNotification
method works, but only when I edit the text in the text field, not then I click it. If I edit the text again, this method does not work. Why?
Sorry, I think I want to use this on mac , not on iphone,How to do with it with cocoa?
The textFieldDidBeginEditing: delegate method only gets triggered when the user starts editing the text inside the UITextField, as the method name implies.
If you want to trigger a method when the UITextField is touched, you should try this:
[textField addTarget:self
action:#selector(textFieldTouched:)
forControlEvents:UIControlEventTouchDown];
- (void) textFieldTouched:(id)sender {
// Display the panel
}
The correct delegate method name is
- (void)textFieldDidBeginEditing:(UITextField *)textField
From the documentation:
This method notifies the delegate that the specified text field just became the first responder.