In my app i'm using a UITextField collect a string value. Whenever i finish editing the field, textFieldDidEndEditing gets fired, but later textFieldShouldReturn never. What can be it's reason?
I provide some code:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if(textField == self.urlPatternTextField) {
/*do some stuff*/
}
}
- (IBAction)dismissKeyboard:(id)sender {
NSMutableArray* possibleReponders = [[NSMutableArray alloc]initWithObjects:
self.urlPatternTextField, nil];
for (UITextView* tv in possibleReponders) {
if([tv isFirstResponder]) {
[tv resignFirstResponder];
return;
}
}
}
Note:
In textFieldDidEndEditing the if condition is true.
Thanks for the help in advance.
Sincerely,
Zoli
textFieldShouldReturn:
Asks the delegate if the text field should
process the pressing of the return button.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
Parameters
textField The text field whose return button was pressed.
Return Value
YES if the text field should implement its default behavior for the
return button; otherwise, NO.
Discussion The text field calls this method whenever the user taps the
return button. You can use this method to implement any custom
behavior when the button is tapped.
The method invokes when return button is pressed.Try it so
Related
I have a custom UIView which creates a UITextField as a subview while the application is running. I have been stuck on this for hours now, but cannot figure out how to create delegates to hide the keyboard and determine when the "done" button is pressed. I'm still pretty new to iOS development, so any help here would be greatly appreciated.
Thanks.
u can do this set
yourtextfieldname.delegate=self;
when u hide your keyboard.. then u can call that textfield and do this code...
[txtfieldname resignFirstResponder];
or u can try this.. also
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[yourtextfieldname resignFirstResponder];
}
or best method
-(BOOL)textfieldshouldreturn:(UITextField *)textField
{
[textField resignFirstResponder];
}
If you are using keyboard done button, set the delegate to the textfield
texfield.delegate = self;
then the below method will be called when done button is clicked
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
If you are using done button on a toolbar created by you then add a target to the doneButton then call below method in the selector
[textField resignFirstResponder];
This may help you.First use UITextFieldDelegate and set
yourtextfield.delegate=self;
Then use
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[yourtextfield resignFirstResponder];
}
// and also you can use this
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[yourtextfield resignFirstResponder];
return YES;
}
Hello i am making an app that uses a login page and registration page. I need to make it so when in the login page for example, i can press the return key (customised to say next) and it will move the cursor to the next text field. I have researched it a lot and have found some code that should work however no matter what i do it won't work.
Code 1 -
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == field1TextField) {
[textField resignFirstResponder];
[field2TextField becomeFirstResponder];
}
else if (textField == field2TextField) {
[textField resignFirstResponder];
[field3TextField becomeFirstResponder];
}
else if (textField == field3TextField) {
[textField resignFirstResponder];
}
return YES;
}
This makes sense to me however it wont work.
My first text field is called txtUsername and my second text field is called txtPassword. So i customized the code like this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == txtUsername) {
[textField resignFirstResponder];
[txtPassword becomeFirstResponder];
}
return YES;
}
I have setup a custom .h & .m file and am placing the code in there. Should i be linking the code to the text field somehow?
In order to call the textFieldShouldReturn method you will need to set the delegate of the UITextField. In your viewDidLoad method, add:
txtUsername.delegate = self;
txtPassword.delegate = self;
I am making a application in which I want keyboard on double-tap, I have disabled keyboard on single tap by using textfield delegate method:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return NO;
}
And after that I make a action method for textfield
[textField1 addTarget:self action:#selector(clicked) forControlEvents:UIControlEventTouchDownRepeat];
And in this method I used this code:
[textField1 becomeFirstResponder];
But this is not working
Suggest me, what should I do?
First create a class variable of type Bool (say BOOL shouldFire) and initialise it to NO.
And add UITapGestureRecogniser with number of taps = 2 to the UITextField.
When the selector attached to TapGesture fires, use below code
-(void)tapped:(UITapGestureRecogniser *)ges{
_shouldFire=YES;
[textField1 becomeFirstResponder];
}
And change your method to->
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(self.shouldFire==YES){
self.shouldFire=NO;
return YES:
}
return NO;
}
Well straight forward suggestion is to add a static variable
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
static int count = 0;
count++;
if(count%2 ==0)
return YES;
else
return NO;
}
I have two UITextFields that I want to use for logging in, one for the username and one for the password. Before I wasn't able to dismiss the keyboard in the first UITextField but I managed to fix that, the problem now is that once it dismisses I cannot click on the other UITextField to enter in the password, I am just stuck in the username UITextField with the keyboard dismissed, so especially I can't do anything. Any suggestions on how to dismiss the keyboard and then click on the other UITextField to enter the information?
#interface LoginViewController () <UITextFieldDelegate>
#end
#implementation LoginViewController
#synthesize managedObjectContext, usernameField, passwordField;
-(void) textFieldDidEndEditing:(UITextField *)textField{
}
-(void) textFieldDidBeginEditing:(UITextField *)textField{
[self.usernameField becomeFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//error occurs here
[self.usernameField resignFirstResponder];
return YES;
}
Just do this, this thing will be simple
Set the tag for the textfields
usernameField.tag=1;
passwordField.tag=2;
Then a delegate method to dismiss keyboard
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[userNameField resignFirstResponder];
[passwordField resignResponder];
}
return YES;
}
Update me the result of this
You have problem with your code at textFieldDidBeginEditing
Everytime you are opening keyboard for username field while begin editing.
-(void) textFieldDidEndEditing:(UITextField *)textField{
if(textField==self.usernameField)
{
[self.passwordfield becomeFirstResponder];
}
}
-(void) textFieldDidBeginEditing:(UITextField *)textField{
// [self.usernameField becomeFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//error occurs here
if(textField==self.passwordField)
{
[self.view EndEditing:YES];
}
return YES;
}
The easiest solution to the above problem is changing the textFieldDidBeginEditing function to:
-(void) textFieldDidBeginEditing:(UITextField *)textField{
[textField becomeFirstResponder];
}
And changing the textFieldShouldReturn to the following
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
The above code allows the user to choose the order of input. That is, the user can input the password first, and then the username. If you want the user to input the username first at all times, you could change the textFieldDidBeginEditing to
-(void) textFieldDidBeginEditing:(UITextField *)textField{
if(textField == self.passwordField && [self.userField.text isEqualToString:#""])
[self.usernameField becomeFirstResponder];
else
[textField becomeFirstResponder];
}
This will ensure that the username field is always selected if it is empty. If it is not, then the program will select whichever textfield the user touched, for editing.
I hope this answers your question, though a little late. I think it is an easier way than using and monitoring tags.
I have 2 UITextField in a ViewController. One brings up a UIDatePicker another brings up a KeyBoard(number pad). I then assign user inputs to two class variables, a NSDate variable and a NSNumber variable, in the following method.
-(void)textFieldDidEndEditing:(UITextField *)textField{
if (textField == self.datePickerTextField){
//update xxtime
self.xxTime = self.datePicker.date;
NSLog(#"...%#",self.xxTime);
}
if (textField == self.numberTextField) {
int val = [textField.text intValue];
self.xxNumber = [NSNumber numberWithInt:val];
NSLog(#"...%#",self.xxNumber);
}
}
If I tap the datePickerTextField first then the numberTextField, this method won't get called after I finish typing in the numberTextField.
So my question is how do I make this method to get called? Should I specify "resignFirstResponder" somewhere?
TIA
It will invoke didEndEditing once another control gains focus
According to the API,
This method is called after the text field resigns its first responder
status.
So if you want didEndEditing to be invoked, you need to call
[textField resignFirstResponder];
Yo are right you need to specify resignFirstResponder
- (BOOL)textFieldShouldReturn:(UITextField *)textField {//Text Field Delegate
if (textField == textField1) {
[textField1 becomeFirstResponder];
}else{
[textField resignFirstResponder];
}
return TRUE; }
this can used with textFieldDidEndEditing also
Please check what iOS you are using. Remember that if you are using iOS 10, there is a method that is replacing the old textFieldDidEndEditing called func textFieldDidEndEditing(_ textField: UITextField, reason: UITextFieldDidEndEditingReason)