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

发布于 2024-11-16 13:23:07 字数 1461 浏览 2 评论 0原文

我正在为 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];
}   

Iam using the following code for the UIDatePicker, whatever the date iam selecting it is being displayed in the label. But i need to close the Picker after selecting the date. Is this possible please suggest me.

I am using the following code:

- (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];
}   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟沫凡尘 2024-11-23 13:23:07

我建议您执行以下操作:

在此处输入图像描述

使用带有“完成”按钮和“取消”按钮的工具栏
...

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

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];

I recommend you to do something like this:

enter image description here

to use a toolbar with a Done button and maybe a Cancel Button
...

This is just a quick implementation, it may help

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];
故事还在继续 2024-11-23 13:23:07

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

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

You Have to use button for click on Background to hide the DATEPICKER from your view.

-(IBAction)backGroundClick:(id)sender
{
    [datePicker removeFromSuperview];
}
痴意少年 2024-11-23 13:23:07

也许比从超级视图中删除更好的是将 userInteraction 设置为 NO
并且您需要“完成”按钮,就像用户准备好完成选择日期的地方一样

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

Maybe better then removing from superview would be setting userInteraction to NO
and you need button for "done", as in where the user is ready to finish chosing date

-(IBAction)buttonDoneClick:(id)sender
{
    [datePicker setUserInteractionEnabled:NO];
}
薄暮涼年 2024-11-23 13:23:07

以下代码将起作用。
点击日期标签将启用 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 外部时,Picker 将从视图中退出。

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

Following code will work.
Tapping on the date label will enable the DatePicker as follows:

- (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]];

}

and using the following method Picker will be resigned from the view on tapping outside the Picker.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [datePicker removeFromSuperview];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文