xcode ios应用程序中的日期选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12601199/
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-15 01:38:49  来源:igfitidea点击:

date picker in ios application

iphoneiosxcodedatepickeruitextfield

提问by Maulik Vekariya

i am new to iPhone development. I want to add a UIDatePickerwhen textfield is selected. two date picker is there.

我是 iPhone 开发的新手。我想在UIDatePicker选择文本字段时添加一个。有两个日期选择器。

- (void)textFieldDidBeginEditing:(UITextField *)aTextField
{

    [aTextField resignFirstResponder];

    pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];

    UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
    pickerView.datePickerMode = UIDatePickerModeDate;
    pickerView.hidden = NO;
    pickerView.date = [NSDate date];

    UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    pickerToolbar.barStyle = UIBarStyleBlackOpaque;
    [pickerToolbar sizeToFit];

    NSMutableArray *barItems = [[NSMutableArray alloc] init];

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexSpace];

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];
    [barItems addObject:doneBtn];

    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];
    [barItems addObject:cancelBtn];

    [pickerToolbar setItems:barItems animated:YES];

    [pickerViewPopup addSubview:pickerToolbar];
    [pickerViewPopup addSubview:pickerView];
    [pickerViewPopup showInView:self.view];
    [pickerViewPopup setBounds:CGRectMake(0,0,320, 464)];
}

I used this but got same value for the two different datepickers. I want that if I press textfield1 then datepicker opens and saves that value in textfield one and same for the second textfield . Thanks.

我使用了这个,但是对于两个不同的日期选择器获得了相同的值。我希望如果我按下 textfield1 然后 datepicker 打开并将该值保存在 textfield one 中,第二个 textfield 相同。谢谢。

采纳答案by Suresh Varma

@Maulik: First thing - (void)textFieldDidBeginEditing:(UITextField *)aTextFieldis called every time the textfield is selected for editing.

@Maulik:- (void)textFieldDidBeginEditing:(UITextField *)aTextField每次选择文本字段进行编辑时都会调用第一件事。

So for both your selection i.e. textfield1 and textfield2 same code is going to be called.

因此,对于您的选择,即 textfield1 和 textfield2,将调用相同的代码。

What you can do is at the tap of done button, Check which textfield has opened up the picker and save the value in that textfield.

您可以做的是点击完成按钮,检查哪个文本字段打开了选择器并将值保存在该文本字段中。

What I meant is take a textfield in .h file UITextField *selectedTextField;

我的意思是在 .h 文件中取一个文本字段 UITextField *selectedTextField;

Then in textFieldDidBeginEditingmethod set it as selectedTextField = aTextField;and at the tap of done button i.e in doneButtonPressedset the selected value for selectedTextField.text. And thats it.. you are done :)

然后在textFieldDidBeginEditing方法中将其设置为selectedTextField = aTextField;并在完成按钮的点击即doneButtonPressed设置所选值selectedTextField.text。就是这样..你完成了:)

回答by user2155906

I have an easy solution.

我有一个简单的解决方案。

Set delegate <UIPickerViewDelegate,UIPickerViewDataSource>

设置委托 <UIPickerViewDelegate,UIPickerViewDataSource>

In .hfile:

.h文件中:

UIDatePicker *dtPicker;
UIActionSheet *pickerViewPopup;

-(IBAction)dt_picker:(id)sender;

In .mfile:

.m文件中:

-(IBAction)dt_picker:(id)sender{

    pickerViewPopup = [[UIActionSheet alloc] init];
    const CGFloat toolbarHeight = 44.0f;
    dtPicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, toolbarHeight, 0, 0)];

    NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
    // if (dateStatus==0) {
        dtPicker.datePickerMode = UIDatePickerModeDate;
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        dtPicker.minimumDate = [NSDate date];
    /* }else{
        datePicker.datePickerMode = UIDatePickerModeTime;
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
        [dateFormatter setDateFormat:@"HH:mm:ss"];

    }*/
    dtPicker.hidden = NO;
    dtPicker.date = [NSDate date];

    [dtPicker addTarget:self action:@selector(LabelChange:) forControlEvents:UIControlEventValueChanged];
    UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, toolbarHeight)];
    pickerToolbar.barStyle = UIBarStyleBlackOpaque;
    [pickerToolbar sizeToFit];
    NSMutableArray *barItems = [[NSMutableArray alloc] init];

    UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(btncancelPressed:)];
    [barItems addObject:btnCancel];

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexSpace];

    flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexSpace];

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];
    [barItems addObject:doneBtn];


    lbl1.text=[NSString stringWithFormat:@"%@",dtPicker.date];


    [pickerToolbar setItems:barItems animated:YES];

    [pickerViewPopup addSubview:pickerToolbar];
    [pickerViewPopup addSubview:dtPicker];
    [pickerViewPopup showInView:self.view.superview];
    [pickerViewPopup setBounds:CGRectMake(0,0,self.view.frame.size.width, 464)];

}


-(void)LabelChange:(id)sender{

    NSDateFormatter* formatter1 = [[NSDateFormatter alloc] init];
    [formatter1 setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    [formatter1 setDateFormat:@"yyyy-MM-dd"];

    NSString  *dateString= [formatter1 stringFromDate:dtPicker.date];
    lbl1.text=dateString;
}

-(void)btncancelPressed:(id)sender{
    [pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
}

-(void)doneButtonPressed:(id)sender{
    [pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
}