How to cancel the keyboard in UISearchbar? - iphone

I'm using UISearchBar in my application and it serves as both an edit field as well as a search. So when I want to disappear the keyboard I have to use cancel button in UISearchBar but I can't have that cancel button on screen, so how could T make keyboard disappear when not used without using cancel button. Please help me as i'm new to iPhone application development.
Thanks in advance!

Use this:
[UISearchBar resignFirstResponder];
Just replace the word UISearchBar with the name of the object you have created.

Are you looking for ways you can dismiss the keyboard or how to actually do that programmatically? If programmatically, then [UISearchBar resignFirstResponder]. If you are looking for a possible way for the user to achieve that you can either make the return button on the keyboard resign its first responder status when pressed, or attach a UIGestureRecognizer to your view and set it up so that when the user clicks outside the keyboard, this keyboard goes away.

simply all you need to do is to get UITextfield control from the UISearchbar and then set UITextfield's delegate to whatever delegate that will perform -(void) textFieldShouldReturn:(UITextField *)textField
-(void)viewDidLoad{
UIView * subView;
NSArray * subViews = [searchbar subviews];
for(subView in subViews)
{
if( [subView isKindOfClass:[UITextField class]] )
{
((UITextField*)subView).delegate=self;
((UITextField*)subView).returnKeyType=UIReturnKeyDone;
break;
}
}
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return TRUE;
}

Related

Dismiss keyboard for UITextView without using Return key

I'm using a UITextView and want to keep the normal usage of Return key, i.e. to insert a new line. But how do I dismiss the keyboard when I can't use the Return key for that?
A lot of people add a UIToolbar with a Done button on it above the keyboard. This is how the Safari app does it as well and in my opinion it is the best way to handle this situation. See a pic here.
To dismiss the keyboard, you just have to do [textField resignFirstResponder];.
Here is an okay example of how to add the UIToolbar when the keyboard shows/hides.
How the user triggers it is a design decision: another button, a swipe gesture?
When it's triggered, call:
[self.textView resignFirstResponder];
You could just use a background tap to hide the keyboard.
A good way to Dismiss keyboard.
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
//NSLog(#"ShouldBeginEditing");
if(textView == indexOneTW)
{
here use uibutton and set its target to a method which contains
[urTextView resignFirstResponder]
}
return TRUE;
}
Make sure you declare support for the UITextViewDelegate protocol.
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText: (NSString *)text
{
if([text isEqualToString:#"\n"])
{
[textView resignFirstResponder];
return NO;
}
return YES;
}
Enjoy Buddy with this code....

UITextField always resignFirstResponder

I am using a UIPickerView and I want my keyboard to never show up for some of my UITextFields. When I call resignFirstResponder on the UITextField after it is touched it doesn't make the keyboard go down.
- (IBAction) txtFieldClicked:(id)sender {
[txtField resignFirstResponder];
}
For each text field where you don't want to be able to show the keyboard, do:
[textField setUserInteractionEnabled:NO];
You can also check a corresponding block in the Interface Builder UI. This prevents the text field from receiving the touch event that would otherwise trigger it to display the keyboard.
Set text field delegate and implement its delegate methods
See UItextFieldDelegate
If you dont want the keyboard to pop up, return NO for all those text fields here
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if([textField isEqualTo:self.myTextField]) {
return NO;
}
return YES;
}

iPhone: Resign Keyboard with Done button action with XIB

hi i am working on UITextview
how to Resign Keyboard, after keyboard "Done button" click Action, which XIB
Thank you
hi if you want answer for, Resign the keyboard of UITextview with default "Done" button on keyboard, then this is answer for that
- (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 NO;
}
// For any other character return TRUE so that the text gets added to the view
return YES;
}
i followed this to resign keyboard of UITextview, if there is any concern inform me
Thank You
Create an IBAction and in IB hook it up to the UITextfield's DidEndOnExit event. Then call the [textViewName resignFirstResponder] to make the keyboard go away.
Alternatively, try using [self.view endEditing:YES] in the IBAction.
To assign the action to the Done button of a keyboard:
In these examples, myTextView is a UITextView that is defined in the header and connected in Interface Builder.
-(BOOL)textViewShouldReturn:(UITextView*)textView {
if (textView == myTextView) {
[textView resignFirstResponder];
}
return YES;
}
To assign it to an external button:
Be sure to define an IBAction in your header, then in Interface Builder, connect the button to this action for touchUpInside:
.h
-(IBAction)dismissKeyboard:(id)sender;
.m
-(IBAction)dismissKeyboard:(id)sender {
[myTextView resignFirstResponder];
}
Call resignFirstResponder on the textview that has focus in order to dismiss the keyboard.

Resign keyboard when leaving textfield

Hey, I have created a textfield within two custom cells. One textfield shows the standard keyboard and the other the shows a pickerview when entered. The problem I have is when I move from the keyboard to pickerview textfield without clicking the "return" button on the keyboard, the keyboard doesn't resign. However when I do it using the "return" the keyboard resigns. Am using:
- (void)textFieldDidEndEditing:(UITextField *)myTextField{
[myTextField resignFirstResponder];
}
and can't work out why this isn't working.
Thanks,
William
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == pickertextfield)
{
[textfield1 resignFirstResponder];
[pickertextfield resignFirstResponder];
}
return YES;
}
Does your view controller adopt <UITextFieldDelegate>? Is it hooked up as the delegate of your text fields?
Try using [self.view endEditing:YES]; before showing the pickerview and see it it helps.

iPhone keyboard, Done button and resignFirstResponder

This is probably a dumb question, but I can't find the answer in the docs. Did the "Done" button on the pop-up keyboard always cause the keyboard to disappear? I see a lot of code around the web like this:
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[theTextField resignFirstResponder];
return YES;
}
When I press the "Done" button, the keyboard pops down and the UITextField resigns first responder.
I'm presuming that pressing the "Done" button didn't used to cause a UITextField to resignFirstResponder, but that behavior changed at some time.
I'm debugging on OS 3.0 - 3.1.3
I made a small test project with just a UITextField and this code
#import <UIKit/UIKit.h>
#interface TextFieldTestViewController : UIViewController
<UITextFieldDelegate>
{
UITextField *textField;
}
#property (nonatomic, retain) IBOutlet UITextField *textField;
#end
#import "TextFieldTestViewController.h"
#implementation TextFieldTestViewController
#synthesize textField;
- (void)viewDidLoad
{
[self.textField setDelegate:self];
[self.textField setReturnKeyType:UIReturnKeyDone];
[self.textField addTarget:self
action:#selector(textFieldFinished:)
forControlEvents:UIControlEventEditingDidEndOnExit];
[super viewDidLoad];
}
- (IBAction)textFieldFinished:(id)sender
{
// [sender resignFirstResponder];
}
- (void)dealloc {
[super dealloc];
}
#end
The text field is an unmodified UITextField dragged onto the NIB, with the outlet connected.
After loading the app, clicking in the text field brings up the keyboard. Pressing the "Done" button makes the text field lose focus and animates out the keyboard. Note that the advice around the web is to always use [sender resignFirstResponder] but this works without it.
In Xcode 5.1
Enable Done Button
In Attributes Inspector for the UITextField in Storyboard find the field "Return Key" and select "Done"
Hide Keyboard when Done is pressed
In Storyboard make your ViewController the delegate for the UITextField
Add this method to your ViewController
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
From the documentation (any version):
It is your application’s
responsibility to dismiss the keyboard
at the time of your choosing. You
might dismiss the keyboard in response
to a specific user action, such as the
user tapping a particular button in
your user interface. You might also
configure your text field delegate to
dismiss the keyboard when the user
presses the “return” key on the
keyboard itself. To dismiss the
keyboard, send the
resignFirstResponder message to the
text field that is currently the first
responder. Doing so causes the text
field object to end the current
editing session (with the delegate
object’s consent) and hide the
keyboard.
So, you have to send resignFirstResponder somehow. But there is a possibility that textfield loses focus another way during processing of textFieldShouldReturn: message. This also will cause keyboard to disappear.
One line code for Done button:-
[yourTextField setReturnKeyType:UIReturnKeyDone];
And add action method on valueChanged of TextField and add this line-
[yourTextField resignFirstResponder];
I used this method to change choosing Text Field
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if ([textField isEqual:self.emailRegisterTextField]) {
[self.usernameRegisterTextField becomeFirstResponder];
} else if ([textField isEqual:self.usernameRegisterTextField]) {
[self.passwordRegisterTextField becomeFirstResponder];
} else {
[textField resignFirstResponder];
// To click button for registration when you clicking button "Done" on the keyboard
[self createMyAccount:self.registrationButton];
}
return YES;
}