0

I have a small subclassed UIView that I use to display a "Connection Timeout" animation to the user when the connection has timed out. I have added some code to the method: scrollViewDidScroll so that my subview, when it is presented, would not scroll when the user scrolls the table. However, when the connection timeout view is done animating, and the view is removed from the superView, whenever the tableview is scrolled, I get a message sent to deallocated instance error. I put in a check to see if the subview was nil but that didnt seem to work. How would I avoid this?

How I present the subview:

// Show No Internet Message
noInternetView = [FCDataController presentNoInternetMessage:self.view];
noInternetViewOrigin = noInternetView.frame.origin;

How the view is displayed in my FCDataController class:

+ (NoInternetView *)presentNoInternetMessage:(UIView *)view {

    NoInternetView *internet = [[NoInternetView alloc] initWithFrame:CGRectMake(0, view.bounds.size.height, view.bounds.size.width, 30) andPosition:NoInternetPositionBottom];
    [view addSubview:internet];

    CGRect position = CGRectMake(0, view.bounds.size.height - 30, view.bounds.size.width, 30);

    [UIView animateWithDuration:0.4
                          delay:0.5
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         internet.frame = position;
                     }
                     completion:^(BOOL finished) {
                         NSLog(@"DONE");
                         [self performSelector:@selector(hideInternetMessageForView:) withObject:view afterDelay:2.0];
                     }];

    return internet;
}

+ (BOOL)hideInternetMessageForView:(UIView *)view {

    NoInternetView *internet = [self NoInternetForView:view];
    if (internet != nil) {

        CGRect remove = CGRectMake(0, view.bounds.size.height, view.bounds.size.width, 30);

        [UIView animateWithDuration:0.5
                              delay:1.0
                            options:UIViewAnimationOptionCurveEaseOut
                         animations:^{
                             internet.frame = remove;
                         }
                         completion:^(BOOL finished) {
                             NSLog(@"DONE");

                             [internet removeFromSuperview];
                         }];
        return YES;
    }
    return NO;
}

+ (NoInternetView *)NoInternetForView:(UIView *)view {

    NoInternetView *internet = nil;
    NSArray *subviews = view.subviews;
    Class internetClass = [NoInternetView class];

    for (UIView  *aView in subviews) {
        if ([aView isKindOfClass:internetClass]) {
            internet = (NoInternetView *)aView;
        }
    }
    return internet;
}

ScorllView method:

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

    if (noInternetView != nil) {

        noInternetView.frame = CGRectMake(self.noInternetViewOrigin.x, self.noInternetViewOrigin.y + self.tableView.bounds.origin.y, noInternetView.frame.size.width, noInternetView.frame.size.height);
    }
}
Jon Erickson
  • 1,876
  • 4
  • 30
  • 73

1 Answers1

0

The view may not be deallocated right away. Try instead: if ([noInternetView superview] != nil) {}