2013-05-03 93 views
0

UIDatePickerView工作正常。它顯示,我可以滾動瀏覽日期,但由於某些原因,我選定的OB.text不斷出現,因爲今天的日期!無論我選擇什麼日期,它顯示爲今天的日期!UIDatePicker選擇日期不出現

我選擇這樣的: enter image description here

的NSLog和標籤顯示爲: enter image description here

下面是代碼:

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

回答

2

您正在使用的UIDatePicker的兩個不同的實例。您需要使用日期選擇器的同一個實例來獲得選擇的日期

做一個屬性的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; 
} 
1

這是因爲你在這個方法中

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

分配的UIDatePicker的新實例所以不要創建新的實例。使用已經創建的datepicker實例。

希望它可以幫助你。

相關問題