目标C:开始日期和结束日期UIDatePicker

时间:2015-11-26 14:20:23

标签: ios objective-c uitextfield uidatepicker

我有两个UIDatePickers和两个UITextField。 一个UIDatePickertextField显示开始日期+时间。 第二个UIDatePickertextField显示结束日期+时间。

我面临的问题是我需要连接两个DatePickers,以便结束日期不能超过开始日期+时间。此外,开始和结束日期+时间之间不能超过24小时。为了达到这个目的,我需要做些什么?如何连接这两个UIDatePicker,如果可能的话,阻止日期+时间相互超越?

点击UIDatePicker(而不是键盘)时会弹出textField

我还在学习,所以请耐心等待。我正在尽我所能去理解。 在任何人告诉我此帖已经存在之前,它不会,而不是我希望UIDatePicker工作的方式。 (请将我的代码与他们的代码进行比较,不同)

我对UIDatePicker的代码是:

.h文件:

@

interface TaakToevoegen : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate, UITextViewDelegate>{
    UIDatePicker *beginTijdPicker;
    UIDatePicker *eindTijdPicker; 
}

//TextField for both begindate and enddate. 
@property (strong, nonatomic) IBOutlet UITextField *beginTijdTextField;
@property (strong, nonatomic) IBOutlet UITextField *eindTijdTextField;

.m文件:

beginTijdPicker=[[UIDatePicker alloc] init];
beginTijdPicker.datePickerMode=UIDatePickerModeDateAndTime;

//Sets time to 24hour notation instead of AM & PM
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"NL"];
[beginTijdPicker setLocale:locale];

[self.beginTijdTextField setInputView:beginTijdPicker];
UIToolbar *toolBar=[[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 320, 44)];
[toolBar setTintColor:[UIColor grayColor]];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                          style:UIBarButtonItemStylePlain
                                                         target:self
                                                         action:@selector(ShowSelectedDate1)];

UIBarButtonItem *space= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                      target:nil
                                                                      action:nil];
[toolBar setItems:[NSArray arrayWithObjects:space, doneBtn, nil]];
[self.beginTijdTextField setInputAccessoryView:toolBar];


    -(void)ShowSelectedDate1{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd MMM HH:mm"];

    self.beginTijdTextField.text=[NSString stringWithFormat:@"%@", [formatter stringFromDate:beginTijdPicker.date]];
    [self.beginTijdTextField resignFirstResponder];
}

Enddate(与begindate相同但名称不同):

eindTijdPicker=[[UIDatePicker alloc] init];
eindTijdPicker.datePickerMode=UIDatePickerModeDateAndTime;

//Sets time to 24hour notation instead of AM & PM
NSLocale *locale1 = [[NSLocale alloc] initWithLocaleIdentifier:@"NL"];
[eindTijdPicker setLocale:locale1];

[self.eindTijdTextField setInputView:eindTijdPicker];
UIToolbar *toolBar1=[[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 320, 44)];
[toolBar1 setTintColor:[UIColor grayColor]];
UIBarButtonItem *doneBtn1=[[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                           style:UIBarButtonItemStylePlain
                                                          target:self
                                                          action:@selector(ShowSelectedDate2)];

UIBarButtonItem *space1= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                       target:nil
                                                                       action:nil];
[toolBar1 setItems:[NSArray arrayWithObjects:space1, doneBtn1, nil]];
[self.eindTijdTextField setInputAccessoryView:toolBar1];

-(void)ShowSelectedDate2{
    NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
    [formatter1 setDateFormat:@"dd MMM HH:mm"];

    self.eindTijdTextField.text=[NSString stringWithFormat:@"%@", [formatter1 stringFromDate:eindTijdPicker.date]];
    [self.eindTijdTextField resignFirstResponder];
}

3 个答案:

答案 0 :(得分:1)

[self.beginTijdTextField setInputView:beginTijdPicker];之后添加此内容:

[self.beginTijdTextField addTarget:self action:@selector(beginTijdDidChange) forControlEvents:UIControlEventEditingChanged];

并添加此方法

-(void)beginTijdDidChange
{
    int oneDay = 1;
    NSDate *maxDateFromBeginig = [beginTijdPicker.date dateByAddingTimeInterval:60*60*24*oneDay]; //24h + beginDate
    NSDate *minDateFromBeginig = [NSDate dateWithTimeInterval:1.0 sinceDate:beginTijdPicker.date]; //1 sec + beginDate
    eindTijdPicker.maximumDate = maxDateFromBeginig;
    eindTijdPicker.minimumDate = minDateFromBeginig;
}

答案 1 :(得分:0)

叶!您可以将最大日期和最小日期设置为第二个UIDatePicker。

[datePicker setMinimumDate:minDate]; //minDate should be first date picker's date
[datePicker setMaximumDate:maxDate]; //maxDate should be 24 hrs grater than the minDate.

答案 2 :(得分:0)

除了已经解决的问题,UI似乎有点复杂。如果知道一次必须在另一次之后0-24小时,为什么不让用户选择那个,即开始日期时间和0-24小时时间间隔作为选择器,然后将结束日期计算为开始+间隔?似乎更容易使用给我。