0

我在我的 iPhone 项目中使用 UIDatePicker。我想在打开日期选择器并单击“完成”按钮后在日期选择器中显示日期。我正在使用这段代码:

[datePicker addTarget:self action:@selector(changeDate:) forControlEvents:UIControlEventValueChanged]

changeDate 是我在其他地方定义的方法,我将 UIControlEventValueChanged 的​​相应代码放在该方法中。但是,即使我没有进行任何更改,我也想在点击“完成”按钮后在 UIDatePicker 中显示该值。我怎样才能做到这一点?

4

1 回答 1

2

您可以使用以下代码....

-(void)showDate{
    menu = [[UIActionSheet alloc] initWithTitle:@"Select Date"
               delegate:self
               cancelButtonTitle:nil
               destructiveButtonTitle:nil
               otherButtonTitles:nil];

    datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
    datePicker.datePickerMode = UIDatePickerModeDate;
    datePicker.minimumDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
    [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]autorelease]];
    [dateFormatter setDateFormat:@"dd MMM yyyy"];
    //[dateFormatter setDateFormat:@"MM/dd/YYYY"];

    //[theDatePicker release];
    [datePicker addTarget:self action:@selector(LabelChange:) forControlEvents:UIControlEventValueChanged];


    dateTool = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    dateTool.barStyle=UIBarStyleBlackOpaque;
    [dateTool sizeToFit];
    NSMutableArray *barItems = [[NSMutableArray alloc] init];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DatePickerDoneClick)];
    [barItems addObject:flexSpace];

    [dateTool setItems:barItems animated:YES];
    [menu addSubview:dateTool];
    [menu addSubview:datePicker];
    [menu showInView:self.view];
    [menu setBounds:CGRectMake(0,0,320, 464)];


    //    [self.view addSubview:datePicker];

    [datePicker release];
}
-(IBAction)DatePickerDoneClick{
    [menu dismissWithClickedButtonIndex:0 animated:YES];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

        return [arrTime count];

}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{

        return [arrTime objectAtIndex:row];

}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

        txtTime.text = [arrTime objectAtIndex:row];

}
于 2012-09-05T05:31:32.873 回答