I have created 5 textFields.
And, create tap in tableView to hide keyboard of textfield
UITapGestureRecognizer *tapTableView = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapTable)];
[_tableView addGestureRecognizer:tapTableView];
-(void)tapTable{
[txt1 resignFirstResponder];
[txt2 resignFirstResponder];
[txt3 resignFirstResponder];
[txt4 resignFirstResponder];
[txt5 resignFirstResponder];
}
But, it's not hide keyboard, it's called tapTable but not hide keyboard.
How can I resolved this problem!
It is happening due to you didn't set your IBOutlet and write more preety code as given below. Don't need to resign each and every text field.
-(void)tapTable{
[self.view endEditing:YES];
}
You don't need to do that.
Just use
[self.view endEditing:YES];
Related
I am trying to remove the keyboard when it is in editing mode of a textview.I have added tap gesture on the main view.But on the click of when editing mode is for textfield then keyboard is removed but when editing mode is for textview then keyboard is not removed.Please tell me how can i tackle this issue?
added tap gesture to the main view.
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSingleTap:)];
[self.main_view setUserInteractionEnabled:true];
[self.main_view addGestureRecognizer:singleFingerTap];
calling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
if([self.txt_username isFirstResponder])
{
[self.txt_username resignFirstResponder];
}
if([self.txt_password isFirstResponder])
{
[self.txt_password resignFirstResponder];
}
}
I have already set the delegate for text view & also i have added the textview protocol.
delete code for set setUserInteractionEnabled =true , u not need that
and add this in your func
-(void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
[self.view endEditing:YES];
}
or u can use library TPKeyboardAvoiding , set for scrollview..its automatic close keyboard when u tap view..so u not need that UITapGestureRecognizer
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[[self view] endEditing:YES];
}
When there are new or changed touches for a given phase, the app object calls one of these methods. Each method takes two parameters: a set of touches and an event.
For more info Click here
i think you just put only textview resingFirstresponder.
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
[self.txt_username resignFirstResponder];
}
use it.
I am using the following code to disable keypad when user tap outside of the textfield. I am able to disable the keypad, but the problem is not able to select anything from drop down list. Please suggest me.
ViewDidLoad:
tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
-(void)dismissKeyboard
{
[self.view removeGestureRecognizer:self.tap];
[textField1 resignFirstResponder];
[textField2 resignFirstResponder];
[textField3 resignFirstResponder];
}
self.tap is nil in the instance of the code you have typed, so when you come to remove it it just fails silently. You are creating the gesture recognizer as tap, not self.tap.
I am currently programming an application for iOS 7, but I have recently come across an interesting error. In one of my UIViews, I have 3 normal UITextFields and two other UITextFields that resignFirstResponder when editing begins. They show a UIDatePicker and UIPickerView as well as a UIToolbar. The error I am having is when I am typing in one of the first 3 text fields, and proceed to click the textfield that ends editing without clicking the UIControl (which is called backgroundTapped:), the keyboard does not disappear. I added a log to see whether or not the text field is resigning the firstResponder status with "canResignFirstResponder", and it is returning "1", yet the keyboard does not disappear, even when changing views until I click one of the top 3 text fields and click the background.
Here is my textFieldDidBeginEditing: and the start of my showRunTypePicker: methods:
textFieldDidBeginEditing:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField becomeFirstResponder];
if (textField.tag == 3005) {
//[textField resignFirstResponder];
//[self.view endEditing:YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.25];
[self showRunTypePicker:self];
[UIView commitAnimations];
}
}
showRunTypePicker:
- (IBAction)showRunTypePicker:(id)sender
{
BOOL canResign = [runTypeField canResignFirstResponder];
NSLog(#"canResign: %hhd", canResign);
[runTypeField endEditing:YES];
[runTypeField resignFirstResponder];
[[self view] endEditing:YES];
[pickerView endEditing:YES];
[pickerView setHidden:YES];
[toolbar setHidden:YES];
[distanceField endEditing:YES];
...
}
I cannot seem to figure out what the problem is. Can anyone help me on this?
EDIT: It's working now. I set the [self showRunTypePicker:self] to [self showRunTypePicker:textField] and moved it to textFieldShouldBeginEditing. Now the keyboard properly disappears.
You have a bug in code.
Read this line
[self showRunTypePicker:self];
Don't you think it should actually be:
[self showRunTypePicker:textField];
Here your intention is to resign keyboard when you tab specific text field .
So you should be actually passing the
textField
instance whose tag is 3005.
Try to use sender as a UITextField and perform your bool check.
Hope that helps.
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;
}
I created a form and the keypad (Numeric only) appears when entering data like your age.
I want the keyboard to disappear when the user taps the background and I want to add a "Done" button in the empty slot under the 7 (next to the zero). (im using the Number Pad keyboard)
I found this example but I have a few questions.
In
-(void)viewDidLoad
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)]; [self.view addGestureRecognizer:tap];
}
-(void)dismissKeyboard
{
[aTextField resignFirstResponder];
[aTextField1 resignFirstResponder];
[aTextField2 resignFirstResponder];
[aTextField3 resignFirstResponder];
}
If I have more than 1 text field in my form.
Will I need to write every textfield in the dismissKeyboard method?
Easy way to do this is to use the method provided in UIView
- (BOOL) endEditing:(BOOL)force;
This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign.
So just do this:
-(void)dismissKeyboard {
[self.view endEditing:YES];
}
and it will support any more text fields you add on your page (under that UIView of course)
You should only send dismissKeyboard to that textField that you are currently editing.
In your code you have got memory leak. Better use this one:
-(void)viewDidLoad
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
[tap release];
}
To check if UITextField is currently in edit mode you can check its property:
A Boolean value indicating whether the text field is currently in edit mode. (read-only)
#property(nonatomic, readonly, getter=isEditing) BOOL editing
For example, you have 3 text fields then dismissKeyboard will look something like this:
-(void)dismissKeyboard
{
UITextField *activeTextField = nil;
if ([textField1 isEditing]) activeTextField = textField1;
else if ([textField2 isEditing]) activeTextField = textField2;
else if ([textField3 isEditing]) activeTextField = textField3;
if (activeTextField) [activeTextField resignFirstResponder];
}
I use the same functionality in many of my apps. Rather than using the GestureRecognizer, I set my view up as a UIControl, rather than a UIView. You can still do the things you'd do with a UIView, but you can also assign IBActions to be performed when interacting with the view.
Here's how to do it:
In Interface Builder, select your view. Then, assign its class to UIControl. (It's probably set up as UIView currently.
In your ViewController for that view, write an IBAction method to detect backgroundTaps. Mine looks like this:
- (IBAction)backgroundTap:(id)sender
{
if ([textField1 isEditing]) {
[textField1 resignFirstResponder];
} else if ([textField2 isEditing]) {
[textField2 resignFirstResponder];
}
}
Finally, in Interface Builder, connect the IBAction you created to the UIControl.
Read this article, it may help you
Writing iOS 4 Code to Hide the iPhone Keyboard (Xcode 4)
Here i give common text field object. and asign reference to it in "textFieldShouldBeginEditing" method. that will common for all text field..
In this case, you need to dismiss one text field that will hide keyboard..
declare textfield object in .h file.
#interface RootViewController : UIViewController
{
IBOutlet UITextField* txt_common;
}
IN .m file
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
[tap release];
}
-(void)dismissKeyboard
{
NSLog(#"hi");
[txt_common resignFirstResponder];
}
#pragma mark TextField methods
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSLog(#"hi++++++++++++++++++");
txt_common=textField;
return YES;
}