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)
Related
I'm working on an Objective-C Xcode project in Xcode 6. I have a textfield and a button that checks what's in the textfield when pressed.
However, I would like the code to run when enter key is pressed instead of the submit button.
I have seen that you can set the key equivalent for a button in Xcode however this option doesn't appear to feature in Xcode 6.
Implement textField delegate method.
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self callYourCodeWantToExecute];
//return YES; // want to hide keyboard
//return NO; // want keyboard
}
You need to:
Set delegate for textField
In your delegate object handle -(BOOL)textFieldShouldReturn:(UITextField *)textField i.e. do what you need to do when return pressed and return YES if you want to text field get return NO if you don't want to handle return for text field.
First make your file delegate for UITextField and then add this method to your code ..
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Follow the below steps.
Set delegate of UITExtfield
Add tag value if there are multiple textfield
Now call the delegate method of UITextfield same as below.
Swift
optional func textFieldShouldReturn(_ textField: UITextField) -> Bool
Hope this helps!
I have a scroll view which contains a UITextField and a UITextView. The UITextField return key is "Next" and when this is pressed I call [myTextView becomeFirstResponder]; A random new line is inserted into my textview so I start on the second line. How do I avoid this?
Also important to note that this does not happen when I tap directly on the UITextView rather than tapping the next key.
Thanks in advance!
One solution to solve this is when you implement the textFieldShouldReturn: delegate method, be sure to return NO, and not YES. By returning NO, the newline isn't passed on to the text field.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// move to next field or whatever you need
[myTextView becomeFirstResponder];
return NO;
}
Okay, this behaviour is due to a bug in iOS when becoming firstresponder within same run loop by using next button. To over come this you should do this manually.
First resign first responder from a textFied, and then make textView as a first responder. like this. Implement this delegate method.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
[textView performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
return YES;
}
Similar idea to performSelector, but using asynchronous dispatch queue (Swift 4 version):
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
DispatchQueue.main.async {
self.myTextView.becomeFirstResponder()
}
return true
}
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;
}
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
I have a few UITextFields (within UITableViewCells) on my UIView with a "Save" UIButton. I want to do some basic validation on the UITextFields when the user clicks the "Save" button.
I have overridden textFieldDidEndEditing to save each of my UITextField data to an instance variable; however, if a user clicks the save button before either clicking the "Return" button of the UIKeyboard or clicking on another UITextField the data in my last UITextField is never saved to my instance variable and validation always fails.
I am looking for a way to trigger an "onBlur" (I know that's a JS thing)-type event to save my string in UITextField to my instance variable.
I've looked through the UITextFieldDelegate Protocol and I do not see anything like this.
Is there a method I may be missing?
to trigger textFieldDidEndEditing on your UITextField, you will need to call
[_txt resignFirstResponder];
were _txt is your UITextField
Please note that if you dont have a reference to _txt and you need to find the first responder in order to resign it
You could use the solution from this question Get the current first responder without using a private API
Then instead of calling
[_txt resignFirstResponder];
you would call
[self.view findAndResignFirstResponder];
Try this
// if we encounter a newline character return
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// enter closes the keyboard
if ([string isEqualToString:#"\n"])
{
[textField resignFirstResponder];
return NO;
}
return YES;
}
which will trigger
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
// Call webservice
return YES;
}