keyboard is not getting dismissed on UITextField - iphone

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.

Related

How to dismiss keyboard on second time selecting first textfield?

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

How do I set the next text field in focus for editing when the user hits return? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to navigate through textfields (Next / Done Buttons)
iOS app “next” key won’t go to the next text field
I have two text fields in my view and I'd like the cursor to move from the email text field to the password text field when the user hits the return key. If the password text field is the one in focus, I'd like the keyboard to hide. Here's what I currently have, but it doesn't work...
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
if(textField == self.emailTextField) {
[self.passwordTextField becomeFirstResponder];
}
else if (textField == self.passwordTextField) {
[textField resignFirstResponder];
}
}
What am I missing? Thanks so much in advance for your wisdom!
The code you have in the textFieldDidEndEditing: method belongs in the textFieldShouldReturn: method.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if(textField == self.emailTextField) {
[self.passwordTextField becomeFirstResponder];
} else if (textField == self.passwordTextField) {
[textField resignFirstResponder];
}
return NO;
}
Well, for the reference we have below two textfields,
UITextField *email_Text;
email_Text.tag = 100;
email_Text.delegate = self;
UITextField *password_Text;
password_Text.tag = 101;
password_Text.delegate = self;
You must implement UITextFieldDelegate in you .h file.
Currently iam not using any allocation methods here now for the textfields. You have to alloc it or making it as outlet if the textfield is in the xib by yourself. I am simply just having two objects for reference only. And also these objects must be globally accessible in the class (i mean you should declare it in the header).
The next step is to implement the textFieldShouldReturn: delegate of UITextField.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if(textField.tag == 100)
{
[password_Text becomeFirstResponder];
}
else if(textField.tag == 101)
{
[textField resignFirstResponder];
}
else
{
; //NOP
}
return YES;
}
Try this.
Happy Coding :)
you miss one thing
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if(textField == self.emailTextField) {
[self.passwordTextField becomeFirstResponder];
[self.emailTextField resignFirstResponder];
} else if (textField == self.passwordTextField) {
[textField resignFirstResponder];
[self.emailTextField becomeFirstResponder];
}
return NO;
}

UItextField Delegate not working

In my iPad app I have two textfields. One displays the normal, default textfield and the other should display a picker as its input view.
Problem is, once I use the txt1 which displays default keyboard and then when I touch the 2nd textField the txt1 keyboard is staying visible.
I have also written [txt1 resignFirstResponder]; [txt2 resignFirstResponder]; while displaying the picker.
I have checked the txt1 IBOutlet connection and the delegate assignment, those seem to be correct.
What am I missing?
Write the following code :
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == txt1)
{
return YES;
}
else
{
return NO; // Write the code for displaying UIPickerView instead of the Keyboard.
}
}
Hope this might solve your issue......
txt2.userInteractionEnabled = NO;
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == txt1)
{
[txt2 resignFirstResponder];
// code for Hide Picker
return YES;
   }
else {
// [txt2 resignFirstResponder];
[txt1 resignFirstResponder];
// code for go in picker
return YES;
}
}
for more information
You have to implement below method to resign Keyboard......
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Have u implemented this method ??
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
In your viewDidLoad method write this,
txt2.inputView = pickerView;
and other resignFirstResponder codes should be placed correctly , by this on Tapping txt2, you will directly get pickerview instead of Keyboard.
did you implement the delegates property of UITextFieldDelegates to in the header file, if not do that and check

How can I make the keyboard of other text field disappear while accessing other text field

I have two text fields in my view. I did it using IB. in second text field i am using the action sheet
After entering the text in textField1 I am in text Field-2.In second text field i am using the action sheet with a picker to select the date. so i resigned the textfield -2 keyboard before opening the action sheet. after i dismiss the action sheet when i tried to resign the keyboard it is not returning.
i used
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textfield1 resignFirstResponder];
return YES;
}
to resign the textfield1 ..
- (void) viewDidLoad
{
//don't forget to add <UITextFieldDelegate> in your .h
firstTextField.delegate=self;
secondTextField.delegate=self;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField.tag==2)
{
//Here you can call function to view your datepicker
return NO; //Will not open keyboard for second textfield.
}
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
You have return current textField textFieldShouldReturn method change your textFieldShouldReturn metod like below
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
You need to Give the tag for TextField,
Within this method,
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.tag == 2)
{
[textField1 resignFirstResponder];
// your action sheet code here
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textfield resignFirstResponder]
return YES;
}
Set tag to every text fields and check them in
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if([textfield.tag == 1])
{
[textFieldFirst resignFirstResponder];
}
else
{
// your action sheet code here
}
return YES;
}

UITextField strange behaviour on resignFirstResponder

Already the second day and cannot figure out the problem,
I've UITabelView with Custom UICellViews, each custom UICellView consists of UILabel and UITextField.
Custom UICellView object allocs UITextField and UILabel in its init method and are released in dealloc.
The number of custom UICellViews in UITableView is 6.
The user scenario is following
When user clicks from from 1 to 5 UITextFields virtual keyboard opens and user types some text
When user clicks on the 6th UITextField if virtual keyboard is active, it should be hidden, and if it is hidden it shall not be displayed.
As implement UITextFieldDelegate protocol in my UIViewController class and set the delegate of each UITextField to self.
My delegate methods are following
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.tag != 6) {
return YES;
} else {
[textField resignFirstResponder];
return NO;
}
}
-(BOOL) textFieldShouldEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
-(void) textFieldDidBeginEditing:(UITextField *)textField {
/* Some code */
}
-(void) textFieldDidEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}
All the functions are properly !
So now, the virtual keyboard is never get hidden, why this happens ?
PS. Similar code has worked on iPhone but this issue exists on iPad.
You need to know which textfield was last used! so you can do [lastUsedTextField resignFirstResponder]
There is a dirty, but working trick.. you can make your textfield the new active UITextField and call resignFirstResponder in the next cycle immediately:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.tag != 6) {
return YES;
} else {
// this will schedule keyboard dismissal for the current text field
dispatch_async(dispatch_get_main_queue(), ^{
[textField resignFirstResponder];
});
return YES; // -> make this one active
}
}
did you seted action for textField?
[YourTextField addTarget:self action:#selector(textFieldDoneEditing:) forControlEvents:UIControlEventEditingDidEndOnExit];
PS set any selector for any ControlEvent