无法在UIDatePicker ios中同时选择日期和时间

时间:2015-10-03 11:03:15

标签: ios objective-c uidatepicker

我准备了一个datepicker如下

dateView =[[UIView alloc]init];
dateView.frame=CGRectMake(0, 100, appDelegate.wVal, 400);
[contentView addSubview:dateView];
datePicker =[[UIDatePicker alloc]init];
datePicker.frame = CGRectMake(0, 100, appDelegate.wVal, 300);
[datePicker setDatePickerMode:UIDatePickerModeDate];
//[datePicker addTarget:self action:@selector(fillDate:) forControlEvents:UIControlEventValueChanged];
doneBtn =[[UIButton alloc]init];
doneBtn.frame= CGRectMake(0, datePicker.frame.origin.y+datePicker.frame.size.height+20, appDelegate.wVal, 50);
[doneBtn setTitle:@"Done" forState:UIControlStateNormal];
[doneBtn  setTintColor:[UIColor blackColor]];
[doneBtn setBackgroundColor:[UIColor greenColor]];
[doneBtn addTarget:self action:@selector(doneAction) forControlEvents:UIControlEventTouchUpInside];
[dateView addSubview:datePicker];
[dateView addSubview:doneBtn];

我的doneAction方法无效。

提前感谢。

3 个答案:

答案 0 :(得分:1)

这是UIControlEventValueChanged控件事件附带的默认行为。对于监视器下控件的任何更改,都会触发操作处理程序。

我建议您在用户界面中添加Done按钮。按下Done按钮后,从日期和时间组件中获取数据并使用它们。

编辑:发布OP'问题陈述中的变化:

首先,通过设置[dateView setUserInteractionEnabled:NO]来尝试一次。这将确保用户触摸到达子视图。

其次,这不是创建UIButton的建议方法。你这样做:

UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[doneButton addTarget:self action:@selector(doneAction) forControlEvents:UIControlEventTouchUpInside];

答案 1 :(得分:0)

您好希望这可以帮助您解决问题...

按照以下步骤操作: -

1)First make one view
2)In that view add uidatepicker
3)Then in that view add two buttons like done or cancel
4)Please referred  following picture [![enter image description here][1]][1]

注意: - 我添加了UIPicker。

enter image description here

完成按钮操作: -

- (IBAction)onDatePickerDoneClick:(id)sender
{
    [self removeSubViewWithPopUpAnimation:self.datePickerView];
    self.selectedDate = [self.datePickerExpense1 date];
    self.strDateValue = [self convertDateToString:self.selectedDate dateformat:[self LocalDateFormat:@"dd MMMM yyyy"]];

    [self.tblDetails deselectRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] animated:YES];
    UITableViewCell *cell = [self.tblDetails cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    [((UILabel *)[cell.contentView viewWithTag:222])  setText:self.strDateValue];
}

答案 2 :(得分:-1)

你尝试过使用它吗?

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@“dd-MM-YYYY HH:mm:ss”];