How to use one date picker for two textfield? - ios

I follow this link but I have two textfield
How I use for two textfield.My code is here Date1 and Date2 are textfields.Here when I select date from textfield1 and textfield2 , textfield1 always takes value from textfield2

You need to set tag on every text field and then identify in donButtonPressed method and set text according to tag.
Otherwise
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if([textField isEqual:Date1])
{
Date1.text=#"YourDate";
}
else
{
Date2.text=#"YourDate";
}
}

To do that i always set a current TextField to know the textField. Then I use the currentTextField to modify the text when the pickerDate is validing by the user.
I don't need to use the textFieldDidEndEditing...
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
_activeTextField = textField;
}
Here you set equals the both textfield... That's why you edit Date1 and Date2 when you validate your date picker.
-(void)textFieldDidBeginEditing:(UITextField *)textField{
Date1 = textField;
Date2 = textField;

you have to use textFieldDelegate for this. have add the below code in viewcontroller.h file.
#interface ViewController : UIViewController<UITextFieldDelegate>
Once you did editing in textfield2, the below method.
- (void)textFieldDidEndEditing:(UITextField *)textField{
textField1.text=textField2.text;
}
use this
dateTextField1.text = [NSDateFormatter localizedStringFromDate:[datePicker date] dateStyle:kCFDateFormatterShortStyle timeStyle:kCFDateFormatterNoStyle];
// Time
dateTextField2.text = [NSDateFormatter localizedStringFromDate:[datePicker date] dateStyle:kCFDateFormatterNoStyle timeStyle:kCFDateFormatterShortStyle];
So, textField1 always takes value from textField2.

Related

iOS - can't get rid of keyboard

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

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)

how to increment or decrement textfield value using uistepper?

I currently have a UITextField which has some value in it and also i have a UIStepper besides it.
I want to know how to use the UIStepper so as increment/decrement the textfield value.
CODE:
the value changed action of the UIStepper
1st I tried this
- (IBAction)changedX:(UIStepper*)sender
{
double value = [sender value];
self.XStepper.value=[self.XCoordinate.text doubleValue];
self.XCoordinate.text=[NSString stringWithFormat:#"%g",value];
NSLog(#"%f",value);
}
then i changed it to this:
if ([sender value]==1) {
double temp= [self.XCoordinate.text doubleValue];
temp=temp+1;
self.XCoordinate.text=[NSString stringWithFormat:#"%g",temp];
}
else
{
double temp= [self.XCoordinate.text doubleValue];
temp=temp-1;
self.XCoordinate.text=[NSString stringWithFormat:#"%g",temp];
}
i am not understanding how do i take the current UITextField value and update it in UIStepper so that current value should be incremented/decremented accordingly.
Thanks
You should not do it in this method. From what I understand, you want to be able to change the text in the UITextfield from the UIStepper and also, if the text in the UITextfield changes, you want to update the Stepper.
You should use the Value Changed event of the UIStepper to update the text field, but when you want to update the actual stepper from the text field, you should have another method.
- (void)updateStepper:(UIStepper *)stepper fromTextField:(UITextField *)textfield {
NSInteger value = [textfield.text integerValue];
[stepper setValue:value];
}
try this code:
- (void)viewDidLoad;
{
[self.stepper setValue:10];
// steppr get valye from textfiled
}
- (IBAction)stepperChanged:(UIStepper *)sender
{
NSUInteger value = sender.value;
NSLog(#"%d",value);
NSLog(#"%#",self.XCoordinate.text);
self.XCoordinate.text = [#(value) stringValue];
[self.stepper setValue:value];
}
-(void)textFieldDidEndEditing:(UITextField *)textField;
{
[self.stepper setValue: self.XCoordinate.text.doubleValue];
}
hope it help You
Set the stepper.value to the default value in viewDidAppear/viewDidLoad
This alternative worked for me. Every time I edit the textfield the stepper.value is set. This way when I tap the stepper it has works with the value it already has, the textfield value or it's own value.
- (IBAction)didTapStepper:(UIStepper *)sender {
self.textFieldName.text = [NSString stringWithFormat:#"%ld", (long)sender.value];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if(textField == self.textFieldName){
self.stepperName.value = [[NSString stringWithFormat:#"%#%#", self.textFieldName.text, string] doubleValue];
}
return YES;
}
EDIT: Don't forget to add the Delegate UITextFieldDelegate,
and assign yourself to it.
self.textFieldName.delegate = self;

clearing a UITextField when the user starts typing

short version: How can I make a UITextField box remove all content on the users first keypress? I don't want the info removed until the user starts typing something. ie, clearing it on begin edit is not good enough.
long version: I have three UITextField that loop around (using the return key and catching the press in the "shouldReturn" method. There is text already in the UITextField, and if the user doesn't type anything and just goes to the next UITextField, the value should stay (default behaviour).
But I want it that if the user starts typing, it automatically clears the text first. Something like having the whole field highlighted, and then typing anything deletes the fiels and then adds the user keypress.
"Clear when editing begins" is no good, because the text is immediately cleared on the cursor appearing in the field. That's not desired. I thought I could use the placeholder here, but that doesn't work as a default, and I can't find a default value property. The Highlighted and Selected properties don't do anything in this regard either.
There is a delegate method called
textFieldDidBeginEditing:(UITextField*) tf{
tf.startedEdinting = YES;
}
textFeildDidEndEditing: (UITextField*) tf {
tf.startedEditing = NO;
}
Add startEditing in a category to UITextField.
Then if value changes clear the field:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField.startEditing){
textField.text = string;
} else {
textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
}
}
You can add the property to the UITextField category in the following way:
.h
#property (nonatomic, assign) BOOL startEditing;
.m
#dynamic startEditing;
- (void) setStartEditing:(BOOL)startEditing_in{
NSNumber* num = [NSNumber numberWithBool:startEditing_in];
objc_setAssociatedObject(self, myConstant, num, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL) startEditing{
NSNumber* num = objc_getAssociatedObject(self, myConstant);
return [num boolValue];
}
Declare a BOOL variable in your .h file like.
BOOL clearField;
And implement the delegate methods like:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
clearField = YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
clearField = NO;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
clearField = NO;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(clearField)
{
textField.text = #""
clearField = NO;
}
}
I want to thank people for their answers, I implemented both of the main methods described here and both worked flawlessly. But I have since come across a much simpler, nicer answer and involves only one line of code :)
In the textField's didBeginEditing method, place [self.textField selectAll:self]; or [self.textField selectAll:nil];
The original answer I found had selectAll:self but this shows the cut/copy/paste menu. If you send nil instead of self the menu doesn't appear.
Adding this one line of code highlights the text on entering the textField (so gives the user a visual cue), and only removes everything once a key is pressed.
Another solution that fulfils the same purpose is by simply using a text field placeholder which is defined as:
The string that is displayed when there is no other text in the text field.
So as soon as the user starts typing, the placeholder text disappears.
That's something you can set from the storyboard, or programmatically. (Yes it took me two hours trying to figure it the harder way.. when the solution was literally one line change of code).
If you want to clear the text one the user interacts with it, there is an option in interface builder to where you can set the text field to "Clear when editing begins."
Try to use the following method.
- (BOOL) textField: (UITextField *)theTextField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string {
if(isFirsttime==YES)
{
textfield.text==#"";
isFirsttime=NO;
}
return YES;
}
Declare and initialize a NSString variable for your textField's initial text
NSString *initialText=#"initial text";
Then implement methods:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if(textField.text isEqualToString:initialText)
{
textField.text=#"";
}
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
if(textField.text isEqualToString:#"")
{
textField.text=initialText;
}
}

UITextField clear when selected, but also restore data if nothing is input

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.

Resources