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