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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6423109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:18:38  来源:igfitidea点击:

How to disable the UIDatePicker after selecting the date?

iosxcodeipaduidatepicker

提问by ferry

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.

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

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

回答by Camo

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

回答by Dinesh Raja

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

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

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

回答by Gyfis

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

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

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

回答by Mystery

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

以下代码将起作用。点击日期标签将启用 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]];

}

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