Reputation: 6523
I have a custom view with a UIDatePicker and a done button attached at the top.
When I press the done button, I would like to get the time that is in the UIDatePicker.
I receive an error when I press the button (which I expect since the sender is a UIBarButtonItem) and changing the date time works as expected.
Other solutions show separate selectors but, I would like (if possible) to call the same selector and get the selected as if it were scrolling in the picker view.
I thought of changing the selector method to receive it as an id
but, that did not work.
This is what I'm doing currently, I feel like it might be something silly:
UIView *v = [[UIView alloc] init];
// Create the date time picker
CGRect pickerFrame = CGRectMake(0, 0, 320, 216);
UIDatePicker *pView = [[UIDatePicker alloc] initWithFrame:pickerFrame];
pView.datePickerMode = UIDatePickerModeTime;
[pView addTarget:self action:@selector(dateTimePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
dateTimePicker = pView;
[v addSubview:dateTimePicker];
// done button
CGRect toolBarFrame = CGRectMake(0, 0, 320, 44);
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:toolBarFrame];
UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(dateTimePickerValueChanged:)];
NSArray *toolBarItems = [NSArray arrayWithObjects:flexibleItem, doneButton, nil];
[toolBar setItems:toolBarItems];
[v addSubview:toolBar];
- (void)dateTimePickerValueChanged:(UIDatePicker *)datePicker {
NSLog(@"time on change: %@", datePicker.date);
Image reference:
Upvotes: 0
Views: 1341
Reputation: 1357
TIME
datePicker=[[UIDatePicker alloc]init];
datePicker.datePickerMode=UIDatePickerModeTime;
[yourField setInputView:datePicker];
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
[toolBar setTintColor:[UIColor grayColor]];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(ShowSelectedDate)];
UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolBar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]];
[yourField setInputAccessoryView:toolBar];
-(void)ShowSelectedDate
{
NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
dateFormat.dateStyle=NSDateFormatterMediumStyle;
[dateFormat setDateFormat:@"hh:mm"];
NSString *str=[NSString stringWithFormat:@"%@",[dateFormat stringFromDate:datePicker.date]];
//assign text to label
yourField =str;
[yourField resignFirstResponder];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
Upvotes: -1
Reputation: 645
The correct way to do this is by creating a NSDate
object which is accessible as per your requirement. Now from UIDatePicker
, create a method for value changed. Whenever this method gets called set the value of date picker .date
property to your NSDate
object.
On press of Done button, use the value of NSDate
object. It will give you the right value.
@interface ViewController(){
NSDate *globalDate;
}
@end
@implementation ViewController
- (void)dateTimePickerValueChanged {
globalDate = datePicker.date;
}
- (IBAction)donePressed {
NSLog(@"%@",globalDate);
}
@end
Upvotes: 0
Reputation: 3677
Write your method without parameter and make your UIDatePicker
as a property or global object for entire class so that you can access it from any where.
Now pass your method to both selectors in this way you will achieve date on done button press.
- (void)dateTimePickerValueChanged {
NSLog(@"time on change: %@", datePicker.date);
}
Upvotes: 2
Reputation: 1137
You're right about what's causing this to crash. I think that separating the selectors is the right path. You could have both selectors calling the same method. That way you don't have any copied code.
Upvotes: 1