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]];
Related
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];
}
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];
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have tried this code:
-(IBAction)btn_strtPeriodclicked:(id)sender{
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 0, 0)];
// ... populate your date picker and format it as you see fit.
[self.view addSubview:datePicker];
}
The pickerView display in but too light color.My questions are:
How to make picker view visible properly.
how to remove picker view.
how to display date in label.
You can't change the appearance of UIDatePicker.
You can store it in a property and when you need to remove it from superview call [datePicker removeFromSuperview];
You need a date formatter to get a string value from date. Some like this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
And for setting the label with date:
self.dateLabel.text = [dateFormatter stringFromDate:datePicker.date];
-(IBAction)btn_strtPeriodclicked:(id)sender
{
UIDatePicker* datePicker1=[[UIDatePicker alloc]initWithFrame:CGRectMake(0, 290, self.view.frame.size.width, 200)];
datePicker1.datePickerMode = UIDatePickerModeDateAndTime;
[self.view addSubview:datePicker1];
datePicker1.hidden = NO;
[datePicker1 addTarget:self
action:#selector(dateChange:)forControlEvents:UIControlEventValueChanged];
NSDateFormatter *formate=[[NSDateFormatter alloc]init];
[formate setDateFormat:#"dd-MMM-yy hh:mm a "];
//set date too your lable here
timeText1.text=[formate stringFromDate:datePicker1.date];
}
-(void)dateChange:(id)sender
{
NSLog(#"date is %#",datePicker1.date);
NSDateFormatter *formate=[[NSDateFormatter alloc]init];
[formate setDateFormat:#"dd-MMM-yy hh:mm a "];
timeText1.text=[formate stringFromDate:datePicker1.date];
}
you ve to create view for date picker and add your date picker in that view initially hide the frame of datepicker view
First create property and connect with IBOutlet for both view and date picker in .h
#property (strong, nonatomic) IBOutlet UIView *datePickerView;
#property (strong, nonatomic) IBOutlet UIDatePicker *dobPicker;
and viewDIdload() remove the datepickerview
-(void)viewDidLoad()
{
[self.datePickerView removeFromSuperview];
}
when you do action show the view
- (IBAction)showDate
{
UIView *toView;
self.datePickerView.tag =42;
toView = self.datePickerView;
toView.frame = CGRectMake(0, 210, 320, 206);
[self.view addSubview:toView];
}
then choose date and convert it to string to show it in label
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setDateFormat:#"dd-MMM-yyyy"];
NSDate *date = [self.dobPicker date];
self.label.text = [formatter stringFromDate:birthday];
to hide in that datepickerview add another customview and custom button right hide datepicker action to again remove the view. your view should be like this and create action for done button
- (IBAction)Done:(id)sender {
for (UIView *view in [self.view subviews])
{
if (view.tag == 42 ) {
[view removeFromSuperview];
}
}
}
1 here for more info see this videosee this video
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.
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;
}