Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dantastic

macrumors 6502a
Original poster
Jan 21, 2011
572
678
In a navigation controller I push out a view with a uidatepicker. There's also a label that I'm updating through a function 'changeDate' this is loaded up as follows

Code:
...
	datePicker.date = [NSDate date];
	[datePicker addTarget:self action:@selector(changeDate:) forControlEvents:UIControlEventValueChanged];
	[pickerView addSubview:datePicker];
	[datePicker release];
...


- (void) changeDate:(id)sender { 
...

- (void) save {
	[self unloadPickerView];
...
	[self.navigationController popViewControllerAnimated:YES];

- (void) unloadPickerView {
 // gets the uiview with the picker view and removes all views from subviews

Everything is working as long as you're playing nice. If you would give the datepicker a good flick and then immediately tap the save button the app crashes.
The app crashes because the save function has completed everything and popped the view back before the datepicker has stopped spinning. The datepicker eventually stop spinning and tries to talk to 'changeDate' which is nowhere to be found and the app duly crashes.

How can I best go about this? Can I invalidate the datepicker. Would I have to have my save function wait for the datepicker to finish before popping back?
 
So the solution is to do a removeTarget:
Code:
- (void) unloadPickerView {
	[self.datePicker removeTarget:self action:@selector(changeDate:) forControlEvents:UIControlEventValueChanged];

For a regular picker (UIPickerView) you can set the delegate to nil, passing an argument to nil is perfectly safe:
Code:
- (void) removePicker {
	self.sizePicker.delegate = nil;
 
You shouldn't have to unload the picker by manually calling your own method. It's typical to implement viewDidUnload (on UIViewController) and set any objects you've retained from your view to nil. Also, make sure you're releasing your date picker in your dealloc.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.