UIPickerView and UIDatePicker are not responding to interaction - ios

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

Related

Row selection in UIPickerView

I want to show the current date on selecting the row of UIDatePickerView on the label, My issue is when I opened my date picker it doesn't show anything on the label on selecting the row but when I move my date picker view, It started updating my label, I am not able to fix it. I want to display the date whenever I touch the date picker view row. I even added TouchUpInside row method also, still, it is not working
Here is my code snippet:
- (IBAction)btn_calendar_event:(id)sender {
// ADD UIDatePicker and open it
if (self.picker) {
[self.toolbar removeFromSuperview];
[self.picker removeFromSuperview];
}
[self hideKeyboard];
self.picker = [[UIDatePicker alloc] init];
self.picker.backgroundColor = [UIColor whiteColor];
[self.picker setValue:[UIColor blackColor] forKey:#"textColor"];
self.picker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.picker.datePickerMode = UIDatePickerModeDate;
forControlEvents:UIControlEventValueChanged];
[self.picker addTarget:self action:#selector(dueDateChanged:)forControlEvents:UIControlEventTouchUpInside];
[self.picker addTarget:self action:#selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged];
}
// dueDateChanged method
-(void) dueDateChanged:(UIDatePicker *)sender {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY-MM-dd"];
NSLog(#"Picked the date %#", [dateFormatter stringFromDate:[sender date]]);
_txtf_birthDate.text = [dateFormatter stringFromDate:[sender date]];
}
// Done button Action
-(void)onDoneButtonClick {
[self dueDateChanged : self.picker];
[self.toolbar removeFromSuperview];
[self.picker removeFromSuperview];
}
The problem is when you're initialising your picker your picker is not settings the date to label so it'll only update when dueDateChanged will be triggered so when you initialise your date picker select the date you want to display as a default date and show it in label like
After [self.picker addTarget:self action:#selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged] Simply add a default date to your picker like :-
self.picker.date=[NSDate date]; // add the default initial date
[self dueDateChanged:self.picker];
According to your requirement "If I want to display the current date on selecting row then how to do it, It is now setting default initial date without even selecting the row of date picker view
" you can add this line anywhre from where you want to show the date
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY-MM-dd"];
_txtf_birthDate.text = [dateFormatter stringFromDate:[NSDate date]];

Hide Keyboard in iOS7 with DatePicker as Keyboard

Currently, I am able to have my keyboard appear when I use a text field in my form. However, I'm not able to hide it. So I always need to shutdown the app and to open it back to change pages. I know there is a way to have like a toolbar to close the Keyboard when you want. However, i don't manage to see how to have this toolbar appear.
However, one of my fields enable a DatePicker View as a Keyboard instead of the natural keyboard.
- (void) initializeTextFieldInputView {
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectZero];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.minuteInterval = 5;
datePicker.backgroundColor = [UIColor whiteColor];
[datePicker addTarget:self action:#selector(dateUpdated:) forControlEvents:UIControlEventValueChanged];
self.DateText.inputView = datePicker;
}
- (void) dateUpdated:(UIDatePicker *)datePicker {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
self.DateText.text = [formatter stringFromDate:datePicker.date];
}
To hide a keyboard either [self.DateText resignFirstResponder] or [self.view endEditing:YES] would work.
To add a toolbar over the keyboard use [self.DateText setInputAccessoryView:yourToolBarView];

iOS system settings with time picker UITextField

I need to add time picker to system settings for my app, Is it possible? I see there is a UITextField but no property with time picker for the UITextField.
You have UIPickerView for showing picker
UITextFieldDelegates for textfield start editing
On click on textfield
Disable Keyboard
Show picker
On picker delegate set the textfield with selected value
Edit : In case of settings page which is managed with a plist.It is impossible to do so since apple doesnt give such wide support on the setings page.You van add settings to the application itself as a setting page and do all this
Initialize a textfield. for example UITextField *txtFld;
//Text Field delegate method
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
UIDatePicker *datePicker = [[UIDatePicker alloc]initWithFrame:FRAME];
datePicker.datePickerMode = UIDatePickerModeTime;
[datePicker addTarget:self action:#selector(changeTextFieldValue:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
}
-(void)changeTextFieldValue:(id)sender
{
NSDate *date = [datePicker date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"hh-mm"];
NSString *dateString = [dateFormatter stringFromDate:date ];
txtFld.text = dateString;
}

UITextField inputView - Can the selected view reference the textfield?

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

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