Using the iPhones Multi-Touch Keyboard Search Button - iphone

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;
}

Related

How to resign or hide the keyboard?

I have a pick list to select a day and a text field to show the selected date. It will be like this...
If i choose any date, it will be like this....
The cross symbol in text field is acheived by the code....
textField.clearButtonMode=UITextFieldViewModeAlways;
Now my problem is, while clicking on this cross button, a keyboard was displayed. This is like....
But i want the cross button only for erase the text field. The keyboard should not come. Is it possible?
On your UITextFieldDelegate, implement the method - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField and return NO;
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return NO;
}
In the textfield's 'Editing did begin' method, add the following:
[UITextField resignFirstResponder];
With this in place, that keyboard won't show up.
Happy coding :)
Try setting the editable property of that textfield to false.
As others have said, you can hide the keyboard through the UITextFieldDelegate protocol and through a [texfField resignFirstResponder] method. Alternatively, as vfn suggested, you can prevent thE keyboard from showing altogether.
For that button though, you are young to want to set the clearButtonMode property of the text field. To see what your available options are, read this: http://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextField_Class/Reference/UITextField.html#//apple_ref/doc/c_ref/UITextFieldViewMode

Handle Keyboard Done pressed event on Iphone

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.

How to make the keyboard go away?

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;
}

Can't get rid of the keyboard in UITextView

I have a table and in one of the cells I keep a UITextView. When the user taps it the keyboard appears. I was trying various ways to get rid of it. My first approach was to detect a tap on the table cell of the UITextView, but since the text view takes most of it, it's not suitable. Then I tried to add a button to the toolbar and whenever the user presses it, the keybord disappears with resignFirstResponder, but it won't work. It seems that only when I'm in the same view as the UITextView resignFirstResponder works. So how can I get rid of the keyboard from a different view?
Thanks.
The method below uses the Return Key to dismiss the UITextView Keyboard.
In your Controller, set the UITextView's Delegate to self
like this:
myTextView.delegate=self;
Then add this method:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
// Any new character added is passed in as the "text" parameter
if ([text isEqualToString:#"\n"]) {
// Be sure to test for equality using the "isEqualToString" message
[textView resignFirstResponder];
// Return FALSE so that the final '\n' character doesn't get added
return FALSE;
}
// For any other character return TRUE so that the text gets added to the view
return TRUE;
}
This should work if you're not using the Return key as a true Return key (e.g. adding new line).
What do you mean "it won't work". If txtView is your UITe4xtView, do you mean that associating the button with an action that has [txtView resignFirstResponder] wont work? I've used this code before and it works for me
Without knowing the size of the controls, nor the intent, it's a bit hard to make a solid recommendation. However, if you're looking for a read-only text view, why not consider a UILabel? If it's going to contain a great deal of read-only text and you need rich formatting, consider a UIWebView with your formatting.
Of course, this answer could be completely inappropriate. Can you clarify your intentions, maybe show a screen shot of the app-in-progress (untapped, of course)?
try unchecking the 'Editable' option in the Text View Attributes window in Interface Builder.
this stops the keyboard from showing up if the user clicks on your TextView
i've use for that kind of problem, the folowing method
- (BOOL)textFieldShouldReturn: tf_some_text_field {
[tf_some_text_field resignFirstResponder];
[tf_another_text_field_in_the_same_view resignFirstResponder];
return YES;
}
hope it helps...

Hiding the Keyboard

I have a UISearchBar and on the delegate method I hide the keyboard when the text field is cleared:
- (void)searchBar:(UISearchBar *)filterBar textDidChange:(NSString *)filterText {
NSLog(#"filter: %#", filterText);
if ([filterText length] == 0) {
NSLog(#"hiding keyboard");
[filterBar resignFirstResponder ];
Now when I use the backspace button to clear out the search term all is good. The keyboard hides when the search turns to empty. Not so when I am pressing the "cross" button to clear out the search field altogether.
Well, not entirely true. I does call resignFirstResponder and hides the keyboard - you just can't see it because it comes right back up. I found this out by observing the keyboard show/hide events.
So how come the keyboard is shown again? How can I prevent this?
I've already tried to walk all subviews of the UISearchBar and also call resignFirstResponder on those ...but unless I did something wrong - that doesn't solve this either.
Update:
In fact I just got the keyboard to not disable the "Done" button :-D ...so I will "stop" going down that road as Kevin suggested. Still I would like to know why the keyboard came back up like this.
I would suggest you stop trying to do this. Hiding the keyboard when the field empties out is completely non-standard behavior and the user won't expect it. In situations like this it's far better to keep your behavior consistent with all the rest of the apps across the system.
I see you've accepted an answer and don't plan to continue in this vein, but I am curious whether you could achieve something like you wanted by implementing this:
- (BOOL)canBecomeFirstResponder
{
return !preventingKeyboardAppearance; // so to speak
}
- (void)searchBar:(UISearchBar *)filterBar textDidChange:(NSString *)filterText
{
// handle text
preventingKeyboardAppearance = YES;
[filterBar resignFirstResponder];
}
I'm not clear under what circumstances you would set preventingKeyboardAppearance back to NO, but I do wonder if this would work.
I basically agree with Kevin, but that doesn't help you so here goes:
Try looping through the subviews of the searchbar and find the sibling which is of the class UITextField. Then either set the delegate property of this text field to your ViewController's class and handle the callback there (e.g. textViewShouldReturn), or simply call resignFirstResponder directly on the text field. The former obviously needs to be done at init/load time while the latter can be done in your existing textDidChange callback.
Here are some more pointers:
http://discussions.apple.com/thread.jspa?threadID=1479468&tstart=0
http://discussions.apple.com/thread.jspa?messageID=8176608
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
[textField resignFirstResponder] is not working some time so use this
[YorTextFieldName resignFirstResponder] it's working correctly not any other
function for hiding key bord