UIDatePicker selected date does not appear - ios

UIDatePickerView is working fine. It shows up, I can scroll through the dates, but for some reason my selectedOB.text keeps coming up as Todays date! No matter what date I choose, it shows as todays date!
I select this:
NSLog and the label shows up as:
Here is the code:
- (IBAction)selectDOB:(id)sender
{
UIActionSheet *selectBirthMY = [[UIActionSheet alloc] initWithTitle:#"Select Date of Birth" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Select", nil];
[selectBirthMY setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[selectBirthMY showInView:[UIApplication sharedApplication].keyWindow];
[selectBirthMY setFrame:CGRectMake(0, 100, 320, 500)];
}
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];
pickerView.datePickerMode = UIDatePickerModeDate;
[pickerView setMinuteInterval:15];
//Add picker to action sheet
[actionSheet addSubview:pickerView];
//Gets an array af all of the subviews of our actionSheet
NSArray *subviews = [actionSheet subviews];
[[subviews objectAtIndex:1] setFrame:CGRectMake(20, 265, 280, 46)];
[[subviews objectAtIndex:2] setFrame:CGRectMake(20, 317, 280, 46)];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
UIDatePicker *DOBPicker = [[UIDatePicker alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, YYYY"];
NSDate *pickedDOB = [DOBPicker date];
NSString *DOB = [dateFormatter stringFromDate:pickedDOB];
NSLog (#"This is DOB %#", DOB);
self.selectedDOB.text=DOB;
}

You are using two different instances of UIDatePicker. You need to use the same instance of datePicker to get the selected date
Make a property for datePicker
#property (nonatomic, strong) UIDatePicker *pickerView;
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
if(!self.pickerView){
self.pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];
self.pickerView.datePickerMode = UIDatePickerModeDate;
[self.pickerView setMinuteInterval:15];
//Add picker to action sheet
[actionSheet addSubview:self.pickerView];
}
//Gets an array af all of the subviews of our actionSheet
NSArray *subviews = [actionSheet subviews];
[[subviews objectAtIndex:1] setFrame:CGRectMake(20, 265, 280, 46)];
[[subviews objectAtIndex:2] setFrame:CGRectMake(20, 317, 280, 46)];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, YYYY"];
NSDate *pickedDOB = [self.pickerView date];
NSString *DOB = [dateFormatter stringFromDate:pickedDOB];
NSLog (#"This is DOB %#", DOB);
self.selectedDOB.text=DOB;
}

This is because you are allocating new instance of UIDatePicker in this method
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
So don't create new instance . Use already created datepicker instance .
Hope it helps you.

Related

No known class method for selector 'datePicker'

I have searched similar questions here to help me solve mine, all in vain. I want the UItextField when clicked to pull up the UIDatePicker, then the user can select a date, and then set it in the UITextfield. However I keep getting an error for 'datePicker'. not sure what else I am supposed to do.
This is the part of my app that calls the datePicker
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[pd resignFirstResponder]; //the textField that you will set the selected date
datePicker = [[UIDatePicker alloc] init]; //declared uidatepicker component
pickerViewDate = [[UIActionSheet alloc] initWithTitle:#"Select the date!"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
datePicker.datePickerMode = UIDatePickerModeDate; //set your spesific mode
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]]; //or another LocaleIdentifier instead of en_US
[dateFormatter setDateFormat:#"dd.MM.yyyy"]; //desired format
[datePicker addTarget:self action:#selector(dateChanged) forControlEvents:UIControlEventValueChanged]; //the function would be fired when user change the date in datePicker
//now preparing the toolbar which will be displayed at the top of the datePicker
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle=UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonClicked)]; //barbutton item is "DONE" and doneButtonClicked action will be fired when user clicks the button.
[barItems addObject:flexSpace]; // set the left of the bar
[pickerToolbar setItems:barItems animated:YES];
[pickerViewDate addSubview:pickerToolbar];
[pickerViewDate addSubview:datePicker];
[pickerViewDate showInView:self.view];
[pickerViewDate setBounds:CGRectMake(0,0,320, 464)]; //you can change the position
}
-(IBAction)dateChanged{
NSDateFormatter *FormatDate = [[NSDateFormatter alloc] init];
[FormatDate setLocale: [[NSLocale alloc]
initWithLocaleIdentifier:#"en_US"]];
[FormatDate setDateFormat:#"dd.MM.yyyy"];
pd.text = [FormatDate stringFromDate:[UIDatePicker datePicker]];
}
You're not going to be able to Google the cause of the errors in your code. You will need to figure it out, as your code is unique.
As #madmik3 points out, this line is wrong:
pd.text = [FormatDate stringFromDate:[UIDatePicker datePicker]];
That code is trying to call a UIDatePicker class method "datePicker", which doesn't make any sense.
What you probably want instead is:
pd.text = [FormatDate stringFromDate: [datePicker date]];
That code is asking your date picker object, in the variable datePicker, for it's date property (the current date set in the date picker.

UIDatePicker as inputView

I have two problems at UIDatePicker as inputView.
1: When I select "April 17 9 00 PM" in datePicker,
dateTextField outputs "2014-04-107 21:00:00".
2: UTC of My country is +0900.
But, NSLog outputs "2014-04-17 12:00:00 +0000".
How do I fix these problems?
#property IBOutlet UITextField* dateTextField;
#property IBOutlet UIDatePicker* datePicker;
#property UIToolbar* toolBar;
- (void)viewDidLoad
{
self.datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 600, 320, 216)];
[self.view addSubview:self.datePicker];
self.toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, -40, 320, 40)];
self.toolBar.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dateTextFieldShouldReturn)];
NSArray* finishBarItems = [NSArray arrayWithObjects:doneButton, nil];
[self.toolBar setItems:finishBarItems animated:YES];
self.dateTextField = [[UITextField alloc]initWithFrame:CGRectMake(40, 293, 250, 30)];
self.dateTextField.delegate = self;
self.dateTextField.inputView = self.datePicker;
self.dateTextField.inputAccessoryView = self.toolBar;
[myScrollView addSubview:self.dateTextField];
}
- (void)dateTextFieldShouldReturn
{
NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy-MM-DD HH:mm:ss"];
NSString* dateString = [formatter stringFromDate:self.datePicker.date];
[self.dateTextField setText:dateString];
[self.dateTextField resignFirstResponder];
NSDate* date = [formatter dateFromString:self.dateTextField.text];
NSLog(#"%#", date);
}
Like this works for me:
- (void)dateTextFieldShouldReturn
{
NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"]];
[formatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss zzz"];
NSString* dateString = [formatter stringFromDate:self.datePicker.date];
[self.dateTextField setText:dateString];
[self.dateTextField resignFirstResponder];
NSDate* date = [formatter dateFromString:self.dateTextField.text];
NSLog(#"%#", date);
}

Getting date from the label onto the datepicker

I am working with the UIdatepicker. I am fetching the time from the datepicker and showing the date on the label. Once i select the date it is shown on the label and datepicker is hidden away. Now i want that when i call the picker it should show me the selection bar of the datepicker on the date of the label which i selected previously selected on the datepicker.Code i am using is shown below:-
-(void)datepickershown:(UILabel *)time animated:(BOOL)animated
{
UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:#"Select the Time" delegate:self cancelButtonTitle:nil destructiveButtonTitle:#"DONE" otherButtonTitles:Nil, nil];
pickdate = [[UIDatePicker alloc] initWithFrame:[actionsheet bounds]];
pickdate.hidden = NO;
//pickdate.date = [NSDate date];
pickdate.datePickerMode = UIDatePickerModeTime;
[pickdate addTarget:self action:#selector(changeDateInLabel:) forControlEvents:UIControlEventValueChanged];
[actionsheet addSubview:pickdate];
[actionsheet showInView:self.view];
[actionsheet setBounds:CGRectMake(0,0,320, 500)];
CGRect pickerRect = pickdate.bounds;
pickerRect.origin.y = -90;
pickdate.bounds = pickerRect;
}
- (IBAction)changeDateInLabel:(id)sender
{
NSLog(#"I Am Changing the values");
df = [[NSDateFormatter alloc] init];
df.timeStyle = NSDateFormatterShortStyle;
}
As you want the datepicker to show the date you have already set on the label.
For this you need to use following method.
- (void)setDate:(NSDate *)date animated:(BOOL)animated
Pass the date whatever you have selected and is shown in the label.
try this
[self.datePicker setDatePickerMode:UIDatePickerModeDate];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MM-yyyy"];
if([dateFormat dateFromString:yourDateLabel.text])
{
NSDate *date = [dateFormat dateFromString:yourDateLabel.text];
[self.datePicker setDate:date];
}
hope this will work

feting label time on the datepicker

I am working with the UIdatepicker. I am fetching the time from the datepicker and showing the date on the label. Once i select the date it is shown on the label and datepicker is hidden away. Now i want that when i call the picker it should show me the selection bar of the datepicker on the date of the label which i selected previously selected on the datepicker.Code i am using is shown below:-
-(void)datepickershown:(UILabel *)time animated:(BOOL)animated
{
UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:#"Select the Time" delegate:self cancelButtonTitle:nil destructiveButtonTitle:#"DONE" otherButtonTitles:Nil, nil];
pickdate = [[UIDatePicker alloc] initWithFrame:[actionsheet bounds]];
pickdate.hidden = NO;
//pickdate.date = [NSDate date];
pickdate.datePickerMode = UIDatePickerModeTime;
[pickdate addTarget:self action:#selector(changeDateInLabel:) forControlEvents:UIControlEventValueChanged];
[actionsheet addSubview:pickdate];
[actionsheet showInView:self.view];
[actionsheet setBounds:CGRectMake(0,0,320, 500)];
CGRect pickerRect = pickdate.bounds;
pickerRect.origin.y = -90;
pickdate.bounds = pickerRect;
}
- (IBAction)changeDateInLabel:(id)sender
{
NSLog(#"I Am Changing the values");
df = [[NSDateFormatter alloc] init];
df.timeStyle = NSDateFormatterShortStyle;
}
Image below can describe you more
use setDate:animated of UIDatePicker...
- (void)setDate:(NSDate *)date animated:(BOOL)animated
to convert your label.text to NSDate:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehaviorDefault];
[dateFormatter setDateFormat:#"hh:mm"]; // depends on 12(hh) or 24(HH)
NSDate *yourDate = [dateFormatter dateFromString:label.text];

What is the event being triggered when I clicked done on UIDatePicker of iOS but the value is unchanged?

I am using a UIDatePicker in my iPhone project. I want to show the date in the datepicker after I open it and click the "Done" button. I am using this code:
[datePicker addTarget:self action:#selector(changeDate:) forControlEvents:UIControlEventValueChanged]
changeDate is a method I defined somewhere else and I put the corresponding codes of UIControlEventValueChanged in that method. However I want to display the value in the UIDatePicker after I hit the "Done" button even if I haven't made any changes. How can I do that?
You can use following code....
-(void)showDate{
menu = [[UIActionSheet alloc] initWithTitle:#"Select Date"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.minimumDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]autorelease]];
[dateFormatter setDateFormat:#"dd MMM yyyy"];
//[dateFormatter setDateFormat:#"MM/dd/YYYY"];
//[theDatePicker release];
[datePicker addTarget:self action:#selector(LabelChange:) forControlEvents:UIControlEventValueChanged];
dateTool = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
dateTool.barStyle=UIBarStyleBlackOpaque;
[dateTool sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(DatePickerDoneClick)];
[barItems addObject:flexSpace];
[dateTool setItems:barItems animated:YES];
[menu addSubview:dateTool];
[menu addSubview:datePicker];
[menu showInView:self.view];
[menu setBounds:CGRectMake(0,0,320, 464)];
// [self.view addSubview:datePicker];
[datePicker release];
}
-(IBAction)DatePickerDoneClick{
[menu dismissWithClickedButtonIndex:0 animated:YES];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [arrTime count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [arrTime objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
txtTime.text = [arrTime objectAtIndex:row];
}

Resources