Keyboard resignfirstresponder when Click Done - objective-c

I have a textfield called searchBox and am trying to get rid of the keyboard if the user either clicks on Done.
Is there an IBAction that I need to know to do this?

You have to make your viewcontroller an UITextFieldDelegate and write in
viewDidLoad:
[searchBox setDelegate:self];
And then write:
- (BOOL)textFieldShouldReturn:(UITextField *)textfield
{
[searchbox resignFirstResponter];
return YES;
}

In your view controller:
- (void)viewDidLoad
{
// ...
searchBox.delegate = self;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[searchBox resignFirstResponder];
return YES;
}
See the UITextFieldDelegate documentation for details (you might want to return NO instead of YES in my example).

Related

Go to next textfield when hit Return

I wrote a textFieldDone: method that's suppose to move the cursor to the next textfield when the Return button is tapped.
- (IBAction)textFieldDone:(id)sender {
[nextTextField becomeFirstResponder];
NSLog(#"in : textFieldDone");
}
I have connected the first textfield's "Did End On Exit" event to the File's Owner and chose the textFieldDone: method.
I also assigned the File's Owner as the textfield's delegate (because I need the view to scroll up/down accordingly so the keyboard won't hide the textfields).
When I run the app on the simulator and tap the return button the first textfield resign first responder and in the log I see that the program didn't go through the textFieldDone: method, but it did go through the textFieldDidEndEditing: method.
I used that method before and had no problem.
Is it because the File's Owner is the textfield's delegate?
You need to write on
- (BOOL) textFieldShouldReturn:(UITextField*) textField
to go to next text field.
Sample code:
-(BOOL) textFieldShouldReturn:(UITextField*) textField
{
if (textField == txt1)
{
[txt1 resignFirstResponder];
[txt2 becomeFirstResponder];
}
if (textField == txt2)
{
[txt2 resignFirstResponder];
}
return YES;
}
Don't forget to add delegate UITextFieldDelegate to your UITextfield.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([textField isEqual:txt1])
{
[txt2 becomeFirstResponder];
}
return true;
}
the above answers are correct, but to make this more general you should use the tag option
UITextField *txt1;
txt1.tag=1;
UITextField *txt2;
txt2.tag=2;
UITextField *txt3;
txt3.tag=3;
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([[textField superview] viewWithTag:textField.tag+1])
{
[[[textField superview] viewWithTag:textField.tag+1] becomeFirstResponder];
}
else{ [textField resignFirstResponder];
}
return true;
}
note: don't use textField with tag 0. because all subViews have tag=0 by default.

UITextView trouble in xcode

So i was reading the description about UITextView's and it says that it automatically hides the keyboard when you press the 'Return' button on the keyboard. But it wasn't working, so I tried creating an
- (IBAction)textViewReturn:(id)sender;
{
[myTextView resignFirstResponder];
}
That did not work either so i tried also doing:
- (BOOL)textViewShouldReturn:(UITextView *)textView
{
[myTextView resignFirstResponder];
return NO;
}
Not sure why the whole deal isn't working in the first place. Wondering if anyone could help?
I don't see anything in the UITextView Class Reference that says it automatically hides the keyboard when you press Return.
Also, there is no textViewShouldReturn: message in the UITextViewDelegate protocol. There is a textFieldShouldReturn: message in the UITextFieldDelegate protocol, but a text view is not a text field.
If you want it to hide the keyboard when the user presses Return, you need to do two things.
First, you need to connect some object - usually your view controller - to the text view's delegate outlet. You can do that in your nib, or you can do it in code, perhaps in your viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
myTextView.delegate = self;
}
Second, you need to implement the textView:shouldChangeTextInRange:replacementText: in your delegate object:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
} else {
return YES;
}
}
Note that if the user pastes in text containing a newline and other characters, this will not catch the newline. It will only notice when the user either taps the Return key, or when he pastes in text containing just a newline.
You can declare the delegate's class as conforming to the UITextViewDelegate protocol, in which case Xcode will helpfully autocomplete the method name. But it will work even if the class doesn't conform to the protocol.
Return button of UITextView is used to mote the cursor to the new line. But if you want to remove the keyboard on return button then please try the following code. It resigns the keyboard when return button is pressed by user. So try following code which definitely solved your problem.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
} else {
return YES;
}
}
Per your comment, if you want to use a UITextField instead of a UITextView, then things remain the same except that in order to hide the keyboard when you hit return, you need to implement the following function in the text field's delegate (make sure that you have set this first):
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}

The UISearchBar doesn't dismiss the keyboard when enter is pressed

The UISearchBar doesn't dismiss the keyboard when enter is pressed, or the user touch somewhere else.
I need to use the remove keyboard button on the bottom right of the iOS keyboard in order to remove the keyboard and invoke:
- (void)searchBarTextDidEndEditing:(UISearchBar *)aSearchBar
How can I fix it?
- (void)searchBarTextDidEndEditing:(UISearchBar *)aSearchBar {
[aSearchBar resignFirstResponder];
}
Also you have to set delegate for the UISearchBar: UISearchBarDelegate
It should work.
Here is sample code http://developer.apple.com/library/ios/#samplecode/ToolbarSearch/Listings/ToolbarSearch_APLToolbarSearchViewController_m.html#//apple_ref/doc/uid/DTS40009461-ToolbarSearch_APLToolbarSearchViewController_m-DontLinkElementID_9
Another option is searchBarSearchButtonClicked we can use.
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
// You can write search code Here
}
Add UISearchBarDelegate in .h
Also set SearchBar's object delegate to self.
you should do add UISearchBarDelegate's method:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
// Do the search...
}
EDIT : Above doesnot work then add this:
[self.view endEditing:YES];
for swift 1.2 keyboard will hide when you click finished , and there is another function for cancel but it's not good to use it as when the user click cancel he might want to search for another word ...
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
Use the following code snippet to close/hide the keyboard when return button is clicked.
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if([text isEqualToString:#"\n"])
{
[searchBar resignFirstResponder];
return NO;
}
return YES;
}
it will work -
objective c -
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[searchBar resignFirstResponder];
}
Swift -
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}

IOS: action with enter key of iPad KeyBoard

I have two textfield, in first textfield I write "Hello" and when I push enter in iPad keyboard, I want that in second textfield appear "World"; How can I use enter to create an action in my application?
You would typically assign your view controller as the text field's delegate and then implement the textFieldShouldReturn: method, e.g.:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
otherTextField.text = #"World"
return YES;
}
You can do that by implementing the UITextFieldDelegate protocol in your controller. For instance you could do something like:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == theFirstTextField && [textField.text isEqualToString:#"Hello"]) {
theSecondTextField.text = #"World";
}
return YES;
}
Set your view controller to be the textfield's delegate then implement
-(BOOL)textFieldShouldReturn:(UITextField *)textField
this gets called when the enter button is pushed on the keyboard.
This is roughly what you'd do. Tweaking to condition around device-type (if you truly want iPad only):
#pragma mark - UITextField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.firstTextField && [textField.text isEqualToString:#"Hello"]) {
self.secondTextField.text = #"World";
}
return YES;
}

How to hide the keyboard in objective-c

I have a simple view with a textbox and a UIButton. When I click the UIButton I simply want to hide the keyboard that is currently in the view. Is this a simple delegate I can add to the controller or something more complex?
Of the answers that exist on SO already I haven't found one that has a full solution for this context. Any help would be great!
Try something like:
[TextField resignFirstResponder];
Where TextField is the name of your text field.
This is how you hide the UITextField when you hit the return button:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// do whatever you have to do
[textField resignFirstResponder];
return YES;
}
This is how you hide when you hit an UIButton:
- (void)hideTextField:(UITextField *)textField {
// do whatever you have to do
[textField resignFirstResponder];
}