UITextField strange behaviour on resignFirstResponder - iphone

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

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

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

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