如何在选择日期后禁用UIDatePicker?

时间:2011-06-21 09:28:56

标签: ios xcode ipad uidatepicker

我使用以下代码作为UIDatePicker,无论选择它的日期是在标签中显示的。但是我需要在选择日期后关闭选择器。这可能请建议我。

我使用以下代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    //Create label
    label = [[UILabel alloc] init]; 
    label.frame = CGRectMake(10, 10, 300, 40);
    label.textAlignment = UITextAlignmentCenter;

    //Use NSDateFormatter to write out the date in a friendly format
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateStyle = NSDateFormatterMediumStyle;
    label.text = [NSString stringWithFormat:@"%@",
                  [df stringFromDate:[NSDate date]]];
    [df release];
    [self.view addSubview:label]; 
    [label release];

    // Initialization code
    datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 250)];
    datePicker.datePickerMode = UIDatePickerModeDate;
    datePicker.hidden = NO;
    datePicker.date = [NSDate date];
    [datePicker addTarget:self
                   action:@selector(changeDateInLabel:)
         forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:datePicker];
    [datePicker release];
}

- (void)changeDateInLabel:(id)sender{
    //Use NSDateFormatter to write out the date in a friendly format
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateStyle = NSDateFormatterMediumStyle;
    label.text = [NSString stringWithFormat:@"%@",
                  [df stringFromDate:datePicker.date]];
    [df release];
}   

4 个答案:

答案 0 :(得分:9)

我建议你做这样的事情:

enter image description here

使用带有完成按钮的工具栏,也可以使用取消按钮 ...

这只是一个快速实施,它可能有所帮助

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 100, 320, 44)];
textField.text         = @"Date";
[self.view addSubview:textField];

UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolbar.barStyle   = UIBarStyleBlackTranslucent;

UIBarButtonItem *itemDone  = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:textField action:@selector(resignFirstResponder)];
UIBarButtonItem *itemSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

toolbar.items = @[itemSpace,itemDone];

UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
datePicker.minimumDate   = [NSDate date];
datePicker.date          = [NSDate date];
[datePicker addTarget:self action:@selector(didChangePickerDate:) forControlEvents:UIControlEventValueChanged];

textField.inputAccessoryView = toolbar;
textField.inputView          = datePicker;

[textField becomeFirstResponder];

答案 1 :(得分:3)

您必须使用按钮单击背景以隐藏视图中的DATEPICKER。

-(IBAction)backGroundClick:(id)sender
{
    [datePicker removeFromSuperview];
}

答案 2 :(得分:2)

也许更好,然后从superview中删除将userInteraction设置为NO 并且您需要“完成”按钮,例如用户准备完成选择日期

-(IBAction)buttonDoneClick:(id)sender
{
    [datePicker setUserInteractionEnabled:NO];
}

答案 3 :(得分:0)

以下代码将有效。 点击日期标签将启用DatePicker,如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];


    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
    tap.numberOfTapsRequired = 1;

    label = [[UILabel alloc] init];
    label.frame = CGRectMake(10, 20, 300, 40);
    label.userInteractionEnabled = YES;
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor redColor];
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateStyle = NSDateFormatterMediumStyle;
    label.text = [NSString stringWithFormat:@"%@",
                  [df stringFromDate:[NSDate date]]];
    [label addGestureRecognizer:tap];
    [tap addTarget:self action:@selector(datePickerView)];

    [self.view addSubview:label];


}

- (void)datePickerView
{
    // Initialization code
    datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 250)];
    datePicker.datePickerMode = UIDatePickerModeDate;
    datePicker.hidden = NO;
    datePicker.date = [NSDate date];
    [self.view addSubview:datePicker];
    [datePicker addTarget:self
                   action:@selector(changeDateInLabel:)
         forControlEvents:UIControlEventValueChanged];
}


- (void)changeDateInLabel:(id)sender{
    //Use NSDateFormatter to write out the date in a friendly format
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateStyle = NSDateFormatterMediumStyle;
    label.text = [NSString stringWithFormat:@"%@",
                  [df stringFromDate:datePicker.date]];

}

并使用以下方法Picker将从点击Petch外部的视图中退出。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [datePicker removeFromSuperview];
}