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

时间:2012-09-26 11:55:52

标签: iphone ios xcode datepicker uitextfield

我是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)];
}

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

2 个答案:

答案 0 :(得分:1)

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

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

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

我的意思是在.h文件中使用文本字段 UITextField *selectedTextField;

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

答案 1 :(得分:1)

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

设置委托<UIPickerViewDelegate,UIPickerViewDataSource>

.h档案中:

UIDatePicker *dtPicker;
UIActionSheet *pickerViewPopup;

-(IBAction)dt_picker:(id)sender;

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