How to dismiss keyboard on second time selecting first textfield? - iphone

I'm trying to dismiss keyboard on textfield did begin editing.
First time selecting that textfield works fine, But in second time after resigning second textfield first textfield doesn't resign keyboard.
I tried hard but not get result
Can any one help me.
Thanks in advance.

You can hide keyboard by own method.
For Eg.
-(IBAction)hideKeyboard:(id)sender {
if (sender == txt1) {
[txt2 becomeFirstResponder];
}
else if (sender == txt2) {
[txt2 resignFirstResponder];
}
}
Bind both of textfields with 'Did End On Exit' method in xib.
After resigning first, it'll focus on 2nd. Then after resigning 2nd, keyboard will be dismissed. you can continue with many textfields in this manner.
It'll work definately.
Thanks.

Implement textField's delegate to all the textFields to your class.
And use this one:
- (BOOL) textFieldDidBeginEditing:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}

try this...
-(void)textFieldDidBeginEditing:(UITextField *)textField{
if (textField == yourTextfield1) {
[textField resignFirstResponder];
}
}

Related

UITextfield keyboard resign

in my uitableview in each cell I'm having uitextfield when user edits the textfield and after pressing any other button on the screen the Keyboard is not resigning. I did the following in textfield delegates
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
textfieldInCell = textField;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
textfieldInCell=nil;// this is the ivar i am using for each textfield in cell.
return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
textfieldInCell=textField;
// do the process...
textfieldInCell=nil
}
I am also calling the shouldReturn delegate function once the user tapping on any other button but the keyboard is not resigning. Where am I going wrong?
Confirm that you bind the delegate of each textfield that you create with the view controller and add one line of code in textfield did end editing :-
-(void)textFieldDidEndEditing:(UITextField *)textField
{
// add the following line
[textField resignFirstResponder];
textfieldInCell=textField;
// do the process...
textfieldInCell=nil
}
have you bind delegate of textfield with your controller? or did you check textFieldShouldReturn calling or not?
i think, binding is missing wit view controller in your case.
thanks
add a line in your tableview where you are adding textfield.
<YOUR_TEXTFIELD>.delegate = YES;
Enjoy Programming
first check that you give the delegate or not and give the delegate to UITextField after that when you call the method of button at that time resign this textfield like bellow...
-(IBAction)yourButton_Clicked(id)sender{
[yourTextField resignFirstResponder];
////your code write here
}
you must try to assign tag value to textfield then in should return method you used that tag to hide the keyboard like this (if you assign the tag -1 then)
-(void)textFieldDidEndEditing:(UITextField *)textField{
if(textField.tag==-1){
[textField resignFirstResponder];
}
}

keyboard is not getting dismissed on UITextField

This may sound a newbie question, however I'm new to iOS dev.
Platform : iPad
I have a UITableView with UITextField, let say they are two.
When pressing on the first one virtual keyboard should appear, but when user tapps on the second UITextField the virtual keyboard should be hidden and data picker view should be displayed.
So here it is, how I did it.
-(void) textFieldDidBeginEditing:(UITextField *)textField {
if (textField.tag == PICKER_VIEW_TAG) {
[textField resignFirstResponder];
} else {
...
}
}
-(void) textFieldDidEndEditing:(UITextField *)textField
{
if (textField.tag != PICKER_VIEW_TAG) {
...
}
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.tag == PICKER_VIEW_TAG) {
[self countriesPickerView];
}
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField.tag == PICKER_VIEW_TAG) {
[textField resignFirstResponder];
} else {
...
}
return YES;
}
So now the question, when I click for the first time on the first UITextField it displays keyboard, but when I switch to second one it does not hide it. Why ? and how to solve this ?
UPDATE : The corresponding textField is not getting selected but i.e. the resign takes place, right ? but the keyboard is not hidden ... why this happens ?
The issue is with your textFieldShouldReturn. If you want it to complete the action, allow it to return while resigning the first responder.
[textField resignFirstResponder];
return YES;
Set the inputView property on UITextField to display views other than keyboard to receive input. In this case, you would set the text field's inputView to be an instance of UIDatePicker. The picker will be displayed with the same keyboard animation automatically and you get to delete a bunch of code. Win/win.

Adding Done functionality to keyboard for UITextField

I have a view with two UITextField, one for username, one for password.
I added functionality so the usernameTF return button jumps to the passwordTF. I also made the return button on the passwordTF "Go" which I want to connect to a IBAction I have which will contain the actual login logic.
I tried hooking up the IBAction to the passwordTF's Did End On Exit method but that just seemed to be getting called when the usernameTF was making the passwordTF the first responder.
I'm using this code to jump from usernameTF to passwordTF:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == self.usernameTextField) {
[self.passwordTextField becomeFirstResponder];
}
[textField resignFirstResponder];
return YES;
}
I tried this but it does't call the method when I press Go/Return:
[self.passwordTextField addTarget:self
action:#selector(loginButtonPressed:)
forControlEvents:UIControlEventEditingDidEndOnExit];
I also tried connecting the IBAction to the UITextField's Editing Did End method which calls the method fine when I press the return button, but it also calls the method when I click any UITextField other than the one I have connected with that method. Any fix?
You can try using this
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
if (textField == txtfEmail)
[txtfPassword becomeFirstResponder];
else {
[self login];
}
return YES;
}
Where [self login] is actually the login method being called. Validating login credential from Server or from Database. txtfEmail is Login Name and txtfPassword being the password field. Hope it helps
#RahulSharma is almost there, but not quite.
It has 2 issues addressed by the following approach:
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
// [textField resignFirstResponder];
if (textField == txtfEmail) {
[txtfPassword becomeFirstResponder];
return NO; // Otherwise the "Return" char is posted onto next view
} else {
[self login];
}
return YES;
}
invoking resignFirstResponder is not necessary
returning NO will ensure that the CR will not be sent to the next control

How to move from one textFields to next textField on tabBar button's Action

In my iphone app I have 6 textFields, and a tabBar (having 3 buttons Next,Previous,Done).
All I need to do is when I click on next button, the focus should move from first textField to next textField.
On Next button's IBAction I write the following code :
if ([txt1 isFirstResponder]==YES) {
NSLog(#"TXT1");
[txt1 resignFirstResponder];
[txt2 becomeFirstResponder];
}
else if ([txt2 isFirstResponder]==YES) {
[txt2 resignFirstResponder];
[txt3 becomeFirstResponder];
}
else if ([txt3 isFirstResponder]==YES) {
[txt3 resignFirstResponder];
[txt4 becomeFirstResponder];
}
else if ([txt4 isFirstResponder]==YES) {
[txt4 resignFirstResponder];
[txt5 becomeFirstResponder];
}
else if ([txt5 isFirstResponder]==YES) {
[txt5 resignFirstResponder];
[txt6 becomeFirstResponder];
}
else if ([txt6 isFirstResponder]==YES) {
[txt6 resignFirstResponder];
}
when I use this code NSLog prints infinite times.
What can be done??
Might be because if txt1 isFirstResponder, you're setting txt2 as firstResponder, then checking if txt2 is first responder. You should put a break; at the end of each if statement.
Oops, its my mistake. I Forgot to connect textField's delegate to Files Owner..
Thank you guys.
Actually, in each if statement, you do not need the resignFirstResponder, since you do not want to dismiss the keyboard each time you move to another field.
Instead, leave the only resignFirstResponder in txt6

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.