我在日期选择器中遇到问题。我什么时候点击日期选择器完成按钮。然后我的代码崩溃了。
-(IBAction)btn:(id)sender
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
datelabel.text = [NSString stringWithFormat:@"%@",
[df stringFromDate:[NSDate date]]];
[df release];
[self.view addSubview:datelabel];
[datelabel release];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 300)];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.hidden = NO;
datePicker.date = [NSDate date];
[datePicker addTarget:self
action:@selector(LabelChange:)
forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
[datePicker release];
}
- (void)LabelChange:(id)sender{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
datelabel.text = [NSString stringWithFormat:@"%@",
[df stringFromDate:datePicker.date]];
}
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleBlackOpaque;
toolbar.frame=CGRectMake(0,4,datePicker.frame.size.width,40);
[datePicker addSubview:toolbar];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(DoneSelected)];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpace,doneBtn,nil]];
[flexibleSpace release];
}