0

UIDatePickerView 工作正常。它出现了,我可以滚动浏览日期,但由于某种原因,我的 selectedOB.text 一直显示为今天的日期!无论我选择什么日期,它都显示为今天的日期!

我选择这个: 在此处输入图像描述

NSLog 和标签显示为: 在此处输入图像描述

这是代码:

- (IBAction)selectDOB:(id)sender 
{
    UIActionSheet *selectBirthMY = [[UIActionSheet alloc] initWithTitle:@"Select Date of Birth" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Select", nil];
    [selectBirthMY setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
    [selectBirthMY showInView:[UIApplication sharedApplication].keyWindow];
    [selectBirthMY setFrame:CGRectMake(0, 100, 320, 500)];
}

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];
    pickerView.datePickerMode = UIDatePickerModeDate;

    [pickerView setMinuteInterval:15];
    //Add picker to action sheet
    [actionSheet addSubview:pickerView];
    //Gets an array af all of the subviews of our actionSheet

    NSArray *subviews = [actionSheet subviews];
    [[subviews objectAtIndex:1] setFrame:CGRectMake(20, 265, 280, 46)];
    [[subviews objectAtIndex:2] setFrame:CGRectMake(20, 317, 280, 46)];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    UIDatePicker *DOBPicker = [[UIDatePicker alloc] init];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MMMM dd, YYYY"];

    NSDate *pickedDOB = [DOBPicker date];
    NSString *DOB = [dateFormatter stringFromDate:pickedDOB];
    NSLog (@"This is DOB %@", DOB);
    self.selectedDOB.text=DOB;
}
4

2 回答 2

2

您正在使用两个不同的 UIDatePicker 实例。您需要使用相同的 datePicker 实例来获取选定的日期

为 datePicker 创建一个属性

@property (nonatomic, strong) UIDatePicker *pickerView;

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    if(!self.pickerView){
        self.pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];
        self.pickerView.datePickerMode = UIDatePickerModeDate;

        [self.pickerView setMinuteInterval:15];
        //Add picker to action sheet
        [actionSheet addSubview:self.pickerView];
    }

    //Gets an array af all of the subviews of our actionSheet

    NSArray *subviews = [actionSheet subviews];
    [[subviews objectAtIndex:1] setFrame:CGRectMake(20, 265, 280, 46)];
    [[subviews objectAtIndex:2] setFrame:CGRectMake(20, 317, 280, 46)];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MMMM dd, YYYY"];

    NSDate *pickedDOB = [self.pickerView date];
    NSString *DOB = [dateFormatter stringFromDate:pickedDOB];
    NSLog (@"This is DOB %@", DOB);
    self.selectedDOB.text=DOB;
}
于 2013-05-03T20:04:06.073 回答
1

这是因为您在此方法中分配了 UIDatePicker 的新实例

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

所以不要创建新实例。使用已经创建的 datepicker 实例。

希望它可以帮助你。

于 2013-05-03T20:13:11.457 回答