Invalidate uidatepicker?

Discussion in 'iOS Programming' started by dantastic, Mar 24, 2011.

  1. dantastic macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #1
    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?
     
  2. dantastic thread starter macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #2
    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;
    
     
  3. ppilone macrumors 6502

    Joined:
    Jan 20, 2008
    #3
    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.
     

Share This Page