I have a text field for a user to enter a string, then another text field that when clicked shows a view that slides up with a date picker inside it.
If the user clicks on the first text field and enters a string using the keyboard and then hits the return button on the keyboard the keyboard goes away. But if they click on the date text field after entering a string in the first text field then the keyboard stays on the screen.
I have tried this:
#property (weak, nonatomic) IBOutlet UITextField *nameField;
#property (weak, nonatomic) IBOutlet UITextField *dateField;
....
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField {
return(![textField isEqual:self.dateField]);
}
It doesn't work.
I also tried this (this is triggered on begin editing when user clicks on dateField):
- (IBAction)dateTextFieldClicked:(id)sender {
[self.nameField resignFirstResponder];
}
and then this:
- (IBAction)dateTextFieldClicked:(id)sender {
[self.view endEditing:YES];
}
and neither of these works either. Is what I'm trying to do even possible? Should I just give up or is there some way that will work?
Try This ..
- (IBAction)dateTextFieldClicked:(id)sender {
[self.nameField resignFirstResponder];
[self.view endEditing:YES]; }
Try this in your viewDidLoad():
[self.nameTextField addTarget:self action:selector#(nameFieldClicked:) forControlEvents:UIControlEventEditingDidEnd]
And correct the parameter in your dataTextFieldClicked() as
- (IBAction)dateTextFieldClicked:(UITextField *)sender {
[sender resignFirstResponder];
}
I just be an iOS Developer when Apple launched Swift, which means the codes I wrote above maybe have some syntax error, but without logical error.
I hope my answer can help you.
There is a easy way, I had the same problem. Take the dateField as a UIButton, not as UITextField. In the Button action set this as follows:
- (IBAction)Date_set:(id)sender
{
[self.view endEditing:YES];
[self YourDatePickerset];
}
Set the title of the button when DatePicker value changed like this
- (void)pickerChanged:(id)sender
{
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
[dateformate setDateFormat:#"dd LLL yyyy"]; // Date formater
NSString *date = [dateformate stringFromDate:[myPicker date]]; // Convert date to string
NSLog(#"date :%#",date);
[dob_btn setTitle:date forState:UIControlStateNormal];
}
Can you use this snippet and let me tell the result
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
make sure delegate is set to self for all textfields
Try this also
EDIT
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[dateField resignFirstResponder];
[nameField resignFirstResponder];
}
Related
I have designed a keyboard on my app Now I have the query is that whenever somebody tap on the textfield the keyboard on the device appear which i don't want
is there any way to disable the keyboard so that the user can see the keyboard designed on this app.
In your ViewController add the delegate of the UITextField :
#interface ViewController : UIViewController <UITextFieldDelegate>
While Creating the UITextField or in ViewDidLoad:
self.myTextField.delegate = self;
In your implementation file:
#pragma mark - UITextFieldDelegate
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return NO;
}
Note that you can set the return of textFieldShouldBeginEditing to NO or YES according to your needs, in your case you may return YES , but provide the logic of displaying your custom keyboard
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return NO;
}
And Needs to add delegate <UITextFieldDelegate>
In this method you also can write code to show your custom keyboard.
lol
Set the inputView to a zero sized view:
textField.inputView = [[UIView alloc] initWithFrame:CGRectZero];
You can use following approach to restrict keyboard display when user tap on it :
Method : 1
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return NO;
}
This method hide the Keyboard and dont show the cursor.
Method : 2
UIView* HideView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)] autorelease];
textField.inputView = HideView;
This method hide the Keyboard, but Cursor is appear.
You can disable user interaction.
textField.userInteractionEnabled = 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 a table with UITextFields, and I want to make it so that when a textfield is selected, the current info is cleared (to allow new input), but if the user decides (after selecting) that they don't want to change it, I want them to be able to click elsewhere and the data from before reappears in the textfield.
Is there an easy way to do this?
A good way to do this that's nice and user friendly is to use the placeholder property. Set up your viewController as the textfield's delegate (as described by Andeh) then add the following code:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
textField.placeholder = textField.text;
textField.text = #"";
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
if (textField.text.length == 0) {
textField.text = textField.placeholder;
}
textField.placeholder = #"";
}
And you're done. If you have multiple UITextFields on the page and you don't want this behaviour for all of them you can add a check to the methods above.
In the textFieldDidBeginEditing delegate method, save the current value to a persisting variable before clearing the UITextField. Then in the didFinishEditing delegate method, if the new length of the user input is 0 set the text back to the stored value!
UITextField Delegate docs for reference.
First I think you have two sets of behaviors here.
The text field must clear the value when you begin editing. This exists: -clearsOnBeginEditing.
The text field must restore the previous text if text is empty. Subclassing seems the better solution.
Here is a possible sample class:
// MyRestoringTextField.h
#interface MyRestoringTextField : UITextField
#end
// MyTextField.m
#interface MyRestoringTextField ()
#property (strong, nonatomic) NSString *previousText;
#end
#implementation MyRestoringTextField
- (BOOL)becomeFirstResponder
{
BOOL result = [super becomeFirstResponder];
self.previousText = self.text;
return result;
}
- (BOOL)resignFirstResponder
{
BOOL result = [super resignFirstResponder];
if (self.text.length == 0)
self.text = self.previousText;
return result;
}
#end
Hope that helps.
To clear and then restore a textField if you fail to make an entry, use the following delegates as such:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[[NSUserDefaults standardUserDefaults] setObject:textField.text forKey:kTextFieldIdentifier];
textField.text = #"";
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if ([textField.text isEqualToString:#""])
textField.text = [[NSUserDefaults standardUserDefaults]
stringForKey:kTextFieldIdentifier];
return YES;
}
As of iOS 8.1, textFieldDidBeginEditing is already receiving a cleared text. You should use
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
to initialized the placeholder field.
I have a table view controller with (among others) a cell that is to represent a date. I followed this post "How do I make a modal date picker that only covers half the screen?" and I have it working with one big exception - I can't get the picker to disappear!
I tried registering for the event UIControlEventTouchUpOutside, but it seems that this is not generated by the picker (or at least in the mode that I am using it).
How can I recognize that the user has finished selecting a date?
Also, is there a way to disable the user from inputting directly into the UITextField? I want to force them to use the picker. I saw this post "Disable blinking cursor in UITextField?", but is there another way?
Reagards,
--John
Try this code.. here I am putting an datepicker to a uitextfield.. it will have a done button at the top right navigation bar.. so by clicking done I will user can dismiss the datepicker.. the another best method is by putting a toolbar above the datepicker having the done button.. Try this it will work.. when changing the datepicker you can populate the text field.. Hope this helps..
see this stackoverflow link https://stackoverflow.com/a/4824319/763747 this will have the datepicker with done button as toolbar above the keybord..
#pragma mark -
#pragma mark - TextField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return TRUE;
}
- (void) textFieldDidBeginEditing:(UITextField *)textField {
itsRightBarButton.title = #"Done";
itsRightBarButton.style = UIBarButtonItemStyleDone;
itsRightBarButton.target = self;
itsRightBarButton.action = #selector(doneAction:);
if ([textField isEqual:itsIncidentDateTextField])
{
itsDatePicker = [[[UIDatePicker alloc] init] autorelease];
itsDatePicker.datePickerMode = UIDatePickerModeDate;
[itsDatePicker addTarget:self action:#selector(incidentDateValueChanged:) forControlEvents:UIControlEventValueChanged];
//datePicker.tag = indexPath.row;
textField.inputView = itsDatePicker;
}
}
- (IBAction) incidentDateValueChanged:(id)sender{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM d, yyyy"];
itsIncidentDateTextField.text = [dateFormatter stringFromDate:[itsDatePicker date]];
[dateFormatter release];
}
I used a different method for disappearing the UIDatePicker.
create a XIB (nib) file and add to it a UIDatePicker then resize the view so it fits only the UIDatePicker, create a property (make it strong and nonatomic) in your ViewController (or whatever class your using, and synthesize of course).
#property (nonatomic, strong) UIView *myDatePickerView;
#synthesize myDatePickerView;
then create a loadDatePickerView method
- (void) loadDatePickerView
{
UINib *nib = [UINib nibWithNibName:kNIBname bundle:[NSBundle mainBundle]];
NSArray *views = [nib instantiateWithOwner:self options:nil];
self.myDatePickerView = [views firstObject];
[myDatePickerView setFrame:CGRectMake(0, 318, 320, 162)];
[self.view addSubview:myDatePickerView];
}
implement the UITextFieldDelegate, and in the textFieldDidBeginEditing method call the loadDatePickerView method,
[self loadDatePickerView];
to make function create a property which is a UIDatePicker instance
#property (nonatomic,strong)
UIDatePicker *myDatePicker;
(synthesize of course)
now create an IBAction like so:
-(IBAction)datePickerValueChanged:(UIDatePicker *)sender
{
myDatePicker = [[myDatePickerView subviews] lastObject];
//now you can do whatever you want with the DatePicker
}
now connect the IBAction to the picker in the XIB file, that way the XIB is now the UIDatePicker instance you created in the VC, if you want it to disappear you can add a UITapGestureRecognizer (in the ViewDidLoad) and the selector will be another IBAction which removes myDatePickerView from its' superView like this:
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dropPicker:)];
[self.view addGestureRecognizer:tap];
[self datePickerValueChanged:myDatePicker];
}
-(IBAction)dropPicker:(id)sender
{
[myDatePickerView removeFromSuperview];
}
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;
}