UITextField inputView - Can the selected view reference the textfield? - ios

If I have a set up like so:
- (void)textFieldDidBeginEditing:(UITextField* )textField {
if (textField.tag == 603) {
datePicker = [[UIDatePicker alloc]init];
[datePicker setDatePickerMode:UIDatePickerModeDate];
[datePicker setDate:[NSDate date]];
[datePicker addTarget:self action:#selector(showDate:) forControlEvents:UIControlEventValueChanged];
textField.inputView = datePicker;
}
}
in the setDate method, can I reference which textField the UIPickerDate is the inputView of? That sounds confusing but like something like this:
- (void) showDate: (UIDatePicker*) myDatePicker {
NSDate* selected = [myDatePicker date];
NSString* date = [selected description];
myTextField = myDatePicker.view //I know this is not a property
}
kind of in the same sense that a UITapGestureRecognizer knows what view is calling it...
Explanation for Woz: I could but it would make the app less dynamic. This is a big form for the installation of medical devices. To create the form I store all the element properties in a dictionary (label, type (text, checkbox, segmented control), form section, etc.) and then each dict in an array in the proper order. I programatically build everything while in the array loop. I guess I could subclass UITextField...but you got to admit it would be nice to know which textfield initiated the inputView

I just subclassed UITextField and got it to work:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.textColor = [colorManager setColor:66.0 :66.0 :66.0];
self.font = [UIFont fontWithName:#"Helvetica" size:18.0];
self.backgroundColor = [UIColor whiteColor];
self.borderStyle = UITextBorderStyleRoundedRect;
self.returnKeyType = UIReturnKeyDone;
self.userInteractionEnabled = YES;
datePicker = [[UIDatePicker alloc]init];
[datePicker setDatePickerMode:UIDatePickerModeDate];
[datePicker setDate:[NSDate date]];
[datePicker addTarget:self action:#selector(setDate:) forControlEvents:UIControlEventValueChanged];
self.inputView = datePicker;
}
return self;
}
- (void) setDate : (UIDatePicker*) myDatePicker {
NSDateFormatter* myDateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setDateFormat:#"MM/dd/yyyy"];
NSString* dateString = [myDateFormatter stringFromDate:myDatePicker.date];
self.text = dateString;
}

The UITextField that triggered the UIDatePicker would be the current firstResponder. You can use this info to identify the textfield that is associated with the currently active UIDatePicker.

Add a tag to UIDatePicker and use the tag to find textField from view.
- (void)textFieldDidBeginEditing:(UITextField* )textField {
if (textField.tag == 603) {
datePicker = [[UIDatePicker alloc]init];
[datePicker setTag:textField.tag];
[datePicker setDatePickerMode:UIDatePickerModeDate];
[datePicker setDate:[NSDate date]];
[datePicker addTarget:self action:#selector(showDate:) forControlEvents:UIControlEventValueChanged];
textField.inputView = datePicker;
}
}
- (void) showDate: (UIDatePicker*) myDatePicker {
NSDate* selected = [myDatePicker date];
NSString* date = [selected description];
UITextField *textField = (UITextField *)[self.view viewWithTag:myDatePicker.tag];
}

Related

How to hide/show date picker on button click?

I have a UIDatePicker and I want to hide and show on UIButton Click .
For this I am using BOOL variable isTouchFirst.but it hide and show on second event of button.
Please resolve this problem.And also check first it work on double click event.
if(isTouchFirst){
isTouchFirst=NO;
self. DatePicker.hidden=NO;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
_DatePicker.datePickerMode = UIDatePickerModeDate;
formatedDate=[[NSString alloc]init];
formatedDate = [dateFormatter stringFromDate:self.DatePicker.date];
self.SelectedDate.text =formatedDate;
[DatePicker setMinimumDate: [NSDate date]];
_goDateForcustmoeDetail=formatedDate;
}
else {
isTouchFirst=YES;
self. DatePicker.hidden=YES;
}
BOOL datePickerHidden;
- (void)viewDidLoad
{
[super viewDidLoad];
datePickerHidden = YES;
// _datePicker = [UIDatePicker alloc...
[_datePicker setHidden: datePickerHidden];
// UIButton *dateHiddenSwitchBtn = [UIButton alloc...
dateHiddenSwitchBtn addTarget:self action:#selector(switchDatePickerHidden) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:dateHiddenSwitchBtn];
}
- (void)switchDatePickerHidden
{
if (datePickerHidden) datePickerHidden = NO;
else datePickerHidden = YES;
[_datePicker setHidden: datePickerHidden];
}
Make it hidden = YES by default
- (void)buttonTapped:(UIButton *)sender {
sender.selected = !sender.selected;
datePicker.hidden = !sender.selected;
}

UIPickerView and UIDatePicker are not responding to interaction

I have a UITextField in a UITableView. I am trying to have a UIPickerView or a UIDatePicker as its input view. Both options are appearing and in case of UIPickerView, it is loading correct data. The problem is that it is not scrollable and user cannot interact with it, choose and scroll between the various options.
Userinteraction is set to yes, delegates are correct and everything is in place.
This is the code:
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
UIDatePicker *datepicker = [[UIDatePicker alloc] initWithFrame:CGRectZero];
[datepicker setDatePickerMode:UIDatePickerModeDate];
textField.inputView = datepicker;
return YES;
}
Why are you set the inputView in this method?
Do this in viewDidLoad. This is a configuration,
doesn't make sense set a particular inputView each time the user focus on that textField:
- (void)viewDidLoad {
[super viewDidLoad];
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(editTextField:) forControlEvents:UIControlEventValueChanged];
[textField setInputView:datePicker];
}
and then:
- (void)editTextField:(id)sender
{
UIDatePicker *picker = (UIDatePicker *)textField.inputView;
[picker setMaximumDate:[NSDate date]];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSDate *eventDate = picker.date;
[dateFormat setDateFormat:#"dd/MM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:eventDate];
textField.text = [NSString stringWithFormat:#"%#",dateString];
}

UIDatePicker combined with UITextField, didselectrow

I have a simple UITextField that holds a date and I want to have a UIDatePicker pop up to be able to change the date. I can get the date picker to pop up, but can't figure out how to access the custom cell class I set up from the #selector...?
// set the text field input to the datepicker
UIDatePicker *myPicker = [[UIDatePicker alloc]init];
myPicker.datePickerMode = UIDatePickerModeDate;
[myPicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
resultscell.textEventDate.inputView = myPicker;
return resultscell;
}
-(IBAction)datePickerValueChanged:(id)sender {
NSLog(#"the date changed");
}
How in the above example would I access the resultscell.textEventDate to change it?
What you need to do is make a property for UIDatePicker, and hook up the IBOutlet. Then you can write something like:
self.datePicker.datePickerMode = UIDatePickerModeDate;
[self.datePicker addTarget:self action:#selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
And then in your dateChanged: method you can write:
- (void)dateChanged:(UIDatePicker *)picker
{
NSLog(#"Date: %#", [picker date]);
}
From there you can do whatever you'd like with the NSDate value that is given to you.

Cannot hide UIDatePicker

For my app, I've attempted to make a UIDatePicker appear when the textfield called time is clicked.
In my viewDidLoad, I initialize it like this:
- (void)viewDidLoad {
time.delegate = self;
[time setValue:[UIColor darkGrayColor] forKeyPath:#"_placeholderLabel.textColor"];
datepicker = [[UIDatePicker alloc] init];
datepicker.datePickerMode = UIDatePickerModeCountDownTimer;
dateformatter= [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:#"HH:mm:ss"];
}
Then, in the textFieldShouldBeginEditing method, I have this:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (textField == time){
button.hidden = NO;
datepicker.hidden = NO;
}
return NO;
}
However, my UIDatePicker still stays hidden, and I am sure the outlets are connected
remove this line from your code and check ,if your picker is placed in Xib.
datepicker = [[UIDatePicker alloc] init];
In textFieldShouldBeginEditing you need to set frame according to your need and than you have to add date picker to your view. for example
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (textField == _time){
CGRect frame = datepicker.frame;
frame.origin.x = 100(for eg,set according to you);
frame.origin.y = 100(for eg,set according to you);
datepicker.frame = frame;
[self.view addSubview:datepicker];
button.hidden = NO;
datepicker.hidden = NO;
}
return NO;
}
remove after you done with this.

How to get value from two textFields?

I have two textFields and I have replaced default tap behavior from keyboard to DatePicker
-(void)updateTextField:(id)sender
{
UIDatePicker *picker = (UIDatePicker*)_editStartDate.inputView;
_editStartDate.text = [NSString stringWithFormat:#"%#",picker.date];
}
- (void)viewDidLoad
{
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date];
[datePicker addTarget:self action:#selector(updateTextField:) forControlEvents:UIControlEventValueChanged];
[_editStartDate setInputView:datePicker];
...
}
This code create datepicker instead of keyboard and update textField with name editStartDate, but I have 2nd textField with name editEndDate and I don't know how to get value in it too.
Do you have any ideas ?
Try this one:
-(void)updateTextField:(id)sender
{
if([_editStartDate isFirstResponder]){
UIDatePicker *picker = (UIDatePicker*)_editStartDate.inputView;
_editStartDate.text = [NSString stringWithFormat:#"%#",picker.date];
}
if([_editEndDate isFirstResponder]){
UIDatePicker *picker = (UIDatePicker*)_editEndDate.inputView;
_editEndDate.text = [NSString stringWithFormat:#"%#",picker.date];
}
}

Resources