How to get Keyboard on double tap on a text field - ios

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

Related

Don't show the keyboard when I tapped in TextField

I am new to Objective C and iOS development in general. I am trying to create an app that would make an UITextField to tapped. I want the keyboard not to show when I tap on the textfield. then I want many times to tap textfield and don't show the keyboard.
I Want to try this code but it didn't work.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}
you can called this functions in delegate methods
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (!textField.inputView) {
//it hides the keyboard, but cursor will show
textField.inputView = [[UIView alloc] initWithFrame:CGRectZero];
}
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
return NO;
}
You can implement delegate method like,
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
//Or just return No
}
Make sure that you have confirm protocol UITextFieldDelegate and you have set delegate property of your textField to self.

When Will textFieldDidEndEditing:textField be called?

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)

Hide Keypad on click of textfield IOS

I am devolepeing an app where i am entering data to a textfield from custom view(not input view).Problem is that i don't want the system keypad to popup when i touch the textfield.I have gone through Apple text programming document but was not able to find the solution.How to achieve this.
You can also use gestures,
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)];
gestureRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:gestureRecognizer];
}
- (void)hideKeyboard {
[self.view endEditing:YES];
}
try this code may help you
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField==/**urs testField on which you want to remove keyboard on click*/ ) {
[textField resignFirstResponder];
//*its depend your requirement
[here you can call yours custom view];
}
return YES;
}
adjust as per req..
Set the userInteractionEnabled property:
//UITextField *text;
text.userInteractionEnabled = NO;
Not sure, what exactly you are expecting. I hope that you need to handle the UITextField delegates but without UIKeyboard : If so, then implement delegate of
-(void)textFieldDidBeginEditing:(UITextField *)sender{
// resign the textfield and do your stuff with the data
}
Assign a IBOutlet property to that particular UITextField
and then in viewDidLoad add the following code [self.YourUITextField setUserInteractionEnabled:NO];
Disable the editing for the particular field
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if ([textField isEqual:myTextField]) {
return NO;
}
return YES;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[self.view endEditing:YES]; // this statement will hide keyboard. Useful when we use many textfields based on tag value.
return YES;
}

UITextField triggers unwanted segue when keyboard is dismissed

I'm using Storyboard with this project and have a UITextField inside a view. The user is able to type in a search term, press return and a segue to a resultsViewController occurs as intended.
The problem I am having is that if for any reason the keyboard gets dismissed, the segue occurs automatically. For example, if the user taps the iPad's drop keyboard key, the segue occurs without a search term... or if the user taps outside the UITextField, the keyboard drops (as intended), but the segue also occurs (not intended).
Here's are the methods I'm using (the UITextField's delegate is set in storyboard); also, I've put in "resignFirstResponder" and "endEditing: YES" messages in several places as I was trying to figure out a solution. Sorry for the mess:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if ([textField.text isEqualToString:#""]) {
[textField resignFirstResponder];
return NO;
}
self.clueString = textField.text;
[textField resignFirstResponder];
return YES;
}
- (BOOL) textFieldShouldEndEditing:(UITextField *)textField {
[self.view resignFirstResponder];
return YES;
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
Well, I feel silly but I'll answer my question in case anyone else has this problem.
To control when the segue should or should not occur, I needed to implement the following method:
-(BOOL) shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
if ([self.searchField.text isEqualToString:#""]) {
return NO;
}
return YES;
}
Have you tried setting this method to always return no? You might need to have a check in there on whether or not to search. (Like your above method)
- (BOOL) textFieldShouldEndEditing:(UITextField *)textField {
[self.view resignFirstResponder];
return YES;
}

ios textFieldDidEndEditing fires, but textFieldShouldReturn doesn't

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

Resources