Go to next textfield when hit Return - objective-c

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.

Related

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()
}

textFieldShouldEndEditing not called when user taps Done Button in UIKeyboard

I do validation of UITextField in textfieldShouldEndEditing delegate method, so every time I change the UITextfield being edited the method is called and the validation is performed.
The return button of the keyboard is configured as Done button. When it is pressed I process the input, but the textfieldShouldEndEditing of the last edited UItextField is never called before, so it is not validated.
This seems strange to me since the normal behavior is the user just hitting done button after entering the last character in the field, but this does not trigger the textFieldShouldEndEditingmethod.
To take on this I have to force validation again in the textfieldShouldReturn method.
Maybe I'm missing some point since I can't find the logic in this.
I have found the problem.
I have to resignFirstResponder in textfieldShouldReturnand then the textfieldShouldEndEditingis called after.
This are my methods. I have two UITextFields. self.usernameis an IBOutlet to one of them with return button configured as NEXT. self.passwordis other IBOutlet pointing to a UITextField with Done return Button.
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if (textField == self.userName) {
return [self validateUserName:textField.text];
}
if (textField == self.password) {
return [self validatePassword:textField.text];
}
//No hay errores de validaciĆ³n
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//In userName return is Next
//In password return is Done
if (textField == self.userName) {
[self.password becomeFirstResponder];
}
[textField resignFirstResponder];
return YES;
}

Moving Onto The Next UITextField When 'Next' Is Tapped

I have an iPad application which has a sign up form within it. The form is very basic and contains only two UITextFields which are for Name & Email address.
The first TextField is for the candidates Name, When they enter their name in and press 'Next' on the keyboard I want this to automatically move to the next Email Address TextField to editing.
Any idea how I can set the next button the keyboard to jump to the next keyboard?
Thanks
You need to make your view controller the UITextField delegate, and implement the UITextField delegate method:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == nameField) {
[textField resignFirstResponder];
[emailField becomeFirstResponder];
} else if (textField == emailField) {
// here you can define what happens
// when user presses return on the email field
}
return YES;
}
Swift version:
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == nameField {
textField.resignFirstResponder()
emailField.becomeFirstResponder()
} else if textField == emailField {
// here you can define what happens
// when user presses return on the email field
}
return true
}
You may also want to scroll your view for the emailField to become visible. If your view controller is an instance of UITableViewController, this should happen automatically. If not, you should read this Apple document, especially Moving Content That Is Located Under the Keyboard part.
Additionally to #lawicko 's answer I often change the button text to give that final finishing touch (e.g. says next when there are more fields and then done when on the last):
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
BOOL isLastTextField = //.. your logic to figure out if the current text field is the last
if (isLastTextField) {
textField.returnKeyType = UIReturnKeyDone;
} else {
textField.returnKeyType = UIReturnKeyNext;
}
}
Swift version of correct answer.
In my experience, you do not need to resignFirstResponder when switching textFields.
In this example, it's just your basic username and password textFields.
The keyboard "return key" in storyboard for username is set to "Next" and the one for password is set to "Done".
Then just connect the delegates for these two text fields and add this extension and you're pretty much done.
extension LoginViewController: UITextFieldDelegate {
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == textFieldPassword {
self.view.endEditing(true)
} else {
textFieldPassword.becomeFirstResponder()
}
return true
}
}
A more consistent and robust way is to use NextResponderTextField
You can configure it totally from interface builder.
All you need to do is
Set the class type of your UITextField to be NextResponderTextField
Then set the outlet of the nextResponderField to point to the next responder it can be anything UITextField or any UIResponder subclass. It can be also a UIButton and the library is smart enough to trigger the TouchUpInside event of the button only if it's enabled.
Here is the library in action:
A Swift 4 extension. Just pass the array of UITextFields and it will connect each one to the next until the last one which resigns the first responder (hides the keyboard):
extension UITextField {
class func connectFields(fields: [UITextField]) {
guard let last = fields.last else { return }
// To reset the targets in case you call this method again to change the connected fields
fields.forEach { $0.removeTarget(nil, action: nil, for: .editingDidEndOnExit) }
for i in 0 ..< fields.count - 1 {
fields[i].returnKeyType = .next
fields[i].addTarget(fields[i + 1], action: #selector(UIResponder.becomeFirstResponder), for: .editingDidEndOnExit)
}
last.returnKeyType = .continue
last.addTarget(last, action: #selector(UIResponder.resignFirstResponder), for: .editingDidEndOnExit)
}
}
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.textFieldName)
{
[self.textFieldName resignFirstResponder];
[self.textFieldPassword becomeFirstResponder];
}
else if (textField == self.textFieldPassword)
{
[self.textFieldPassword resignFirstResponder];
[self login:self];
}
return true;
}
#interface MLLoginViewController ()<UITextFieldDelegate>
#end
#implementation MLLoginViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.textFieldName.delegate = self;
self.textFieldPassword.delegate = self;
Make an outlet for the textfield, then
viewController.h
(IBAction)textFieldDoneEditing:(id)sender;
viewController.m
(IBAction)textFieldDoneEditing:(id)sender {
[textField resignFirstResponder];
if (textField == nameField) {
[emailField becomeFirstResponder];
}
}
Make the relation between (show the connections inspector > Sent Events)didEndOnExit and textFieldDoneEditing

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

Keyboard resignfirstresponder when Click Done

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).