I just came across this *problem* as I created a UIView with a date picker and a uitoolbar. I'd have the view slide up and down the screen. Not exactly sure how to go about this I found some code on dem internets and did what any budding coder does, cmd+c, cmd+v.
After some tuning to suit my needs the code works perfectly.
Being a good memory citizen I went on the usual quest to make sure there were no lose ends when I started asking myself - Why does this work???
So far so good, I get what is happening here. I clean up all the objects I allocate and I simply dump a view into the subview. In my understanding, the contents of this subview should be somewhat orphaned from the objects that was used to create them? Here's what I don't get:
If I have already released 'datePicker', howcome the above works?
What is the correct way of doing this?
After some tuning to suit my needs the code works perfectly.
Being a good memory citizen I went on the usual quest to make sure there were no lose ends when I started asking myself - Why does this work???
Code:
@interface {
UIDatePicker *datePicker;
UIView *pickerView;
}
@property (nonatomic, retain) UIDatePicker *datePicker;
@property (nonatomic, retain) UIView *pickerView;
...
- (void) loadDatePicker {
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 320, 250)];
[datePicker addTarget:self
action:@selector(changeDate:) forControlEvents:UIControlEventValueChanged];
...
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
...
pickerView = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 320, 260)];
[pickerView addSubview:toolBar];
[pickerView addSubview:datePicker];
[toolBar release];
[datePicker release];
[self.view addSubview:pickerView];
[pickerView release];
Code:
- (void) changeDate:(id)sender {
// should this not crash my app??
cellLabel.text = [datePicker date];
}
What is the correct way of doing this?