13

This is a new problem I have been having ever since I've been updating my app for iOS 7. Everytime I launch the app on my device or simulator, I get this error code

RecipeDetailViewController scrollViewDidScroll:]: message sent to deallocated instance 0x15746c40 and it crashed.

I enabled NSZombie and that was the code it gave me. Before it was giving a exc_bad_access code.

This is my code for ScrollViewDidSCroll

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{


    // Depending on how far the user scrolled, set the new offset.
    // Divide by a hundred so we have a sane value. You could adjust this
    // for different effects.
    // The larger you number divide by, the slower the shadow will change

    float shadowOffset = (scrollView.contentOffset.y/1);

    // Make sure that the offset doesn't exceed 3 or drop below 0.5
    shadowOffset = MIN(MAX(shadowOffset, 0), 0.6);

    //Ensure that the shadow radius is between 1 and 3
    float shadowRadius = MIN(MAX(shadowOffset, 0), 0.6);



    //apply the offset and radius
    self.navigationController.navigationBar.layer.shadowOffset = CGSizeMake(0, shadowOffset);
    self.navigationController.navigationBar.layer.shadowRadius = shadowRadius;
    self.navigationController.navigationBar.layer.shadowColor = [UIColor blackColor].CGColor;
    self.navigationController.navigationBar.layer.shadowOpacity = 0.2;
}

3 Answers3

23

Another (inelegant, imo) solution is to set your table's delegate to nil on dealloc:

- (void)dealloc {
    [_tableView setDelegate:nil];
}

Seems to be a bug, but I can't guarantee. I am still looking for a reasonable explanation for that.

Note: that probably applies for any UIScrollView subclass, not only UITableView.

Guilherme
  • 7,480
  • 8
  • 49
  • 94
  • 1
    You are a life saver sir. – Reaper Oct 22 '14 at 10:39
  • 1
    This is absolutely the correct answer!!! I discovered that setting the `delegate=nil` was the solution, but tried to put it in `viewWill/DidDissappear` and in other tons of places... this saved my day! – Merlevede Aug 16 '15 at 01:49
7

I was facing the exact situation. I enabled NSZombie, gave me the exact same error in ios7.

In viewDidLoad

[self setEdgesForExtendedLayout:UIRectEdgeNone];

Solved my crash. You can also try from storyboard unselect the Extended Edges >> Under top bars.

see the answer here https://stackoverflow.com/a/18892358/1378447

N.B. It is still a mystery to me why 'scrollViewDidScroll' delegate method is being called even after dealloc.

Community
  • 1
  • 1
Warif Akhand Rishi
  • 22,060
  • 6
  • 76
  • 101
1

or if you watching for table view scrolling

- (void)viewWillDisappear:(BOOL)animated
{
    _table.delegate = nil;
}

Anyway its strange to call notification or something like that on dealloc enter image description here

Mike Glukhov
  • 1,493
  • 16
  • 15