How to tell if a UITextField is firstResponder - iphone

I'm working on an app that has a signup feature. The signup process is broken up into 3 different views, each of which has 2 UITextField to take user input. I have also implemented the UITextFieldDelegate methods;
- (void)textFieldDidBeginEditing:(UITextField *)textField;
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;
- (void)textFieldDidEndEditing:(UITextField *)textField;
and
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
Depending on which UITextField is in focus will determine what method is called when the user taps the return key on the keyboard. I would like to just call my resignFirstResponder method for the first 4 UITextFields but on the last textField I would like to call my join method when return is pressed.
So my question is; How can I determine which UITextField just called the textFieldShouldReturn: method?
As always, thanks in advance!

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == theLastTextField) {
//joinmethod
} else {
[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];
}
}

Which TextFieldDelegate method should be called?

In my application , I want to show one popover view, whenever I am clicking in textfield.
Which text field delegate method should I call?
you have to write your functionality in
-(void)textFieldDidBeginEditing:(UITextField *)textField{
// became first responder
}
Use on the UITextFieldDelegate protocol......
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
NSLog(#"Editing Should Editing"); // SAVE PROCEDURE
}

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

iPhone SDK: How to dismiss keyboard each time user moves to new field

We want the keyboard to be dismissed each time the user moves to a new field. What event would be appropriate to place a resignFirstResponder call?
Thanks.
Your most likely looking for the textFieldShouldReturn delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
Use the appropriate UITextFieldDelegate method:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder]
return YES;
}

iOS: how to send the keyboard down after typing in textField

I want to send the keyboard down after writing in textField.
How can I do this?
Any help would be appreciated.
Implement the - (BOOL)textFieldShouldReturn:(UITextField *)textField; delegate method and tell the text field to resign it's first responder status.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
STEP-1:
You need to set the delegate of the textField to File's Owner (using interface builder)
or
textField.delegate = self; from the viewController programmatically first
STEP-2:
And you implement the method:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Hope this helps.
- (IBAction)textFieldDoneEditing:(id)sender{
[sender resignFirstResponder];
}
connect this to didEndOnExit action of your textfield