0

我有一个小的子类 UIView,用于在连接超时时向用户显示“连接超时”动画。我在方法中添加了一些代码:scrollViewDidScroll,这样当用户滚动表格时,我的子视图在呈现时不会滚动。但是,当连接超时视图完成动画并且从 superView 中删除视图时,每当滚动 tableview 时,我都会收到一条消息发送到 deallocated instance 错误。我检查了子视图是否为零,但这似乎不起作用。我将如何避免这种情况?

我如何呈现子视图:

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

视图如何在我的 FCDataController 类中显示:

+ (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 方法:

-(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);
    }
}
4

1 回答 1

0

视图可能不会立即被释放。改为尝试: if ([noInternetView superview] != nil) {}

于 2013-03-13T01:00:20.803 回答