【发布时间】:2017-01-09 21:40:01
【问题描述】:
我有一个自定义视图,其中有一个 UIDatePicker 和一个附加在顶部的完成按钮。
当我按下完成按钮时,我想获得 UIDatePicker 中的时间。
当我按下按钮时收到错误消息(我认为这是因为发件人是 UIBarButtonItem)并且更改日期时间按预期工作。
其他解决方案显示单独的选择器,但是,我希望(如果可能)调用相同的选择器并像在选择器视图中滚动一样获取被选中的对象。
我想更改选择器方法以将其作为id 接收,但没有奏效。
这就是我目前正在做的事情,我觉得这可能很愚蠢:
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);
图片参考:
【问题讨论】:
标签: ios objective-c uidatepicker