Reputation: 335
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]];
}
Upvotes: 0
Views: 534
Reputation: 131418
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.
Upvotes: 1