无法删除子视图?

时间:2016-12-14 17:04:07

标签: ios uiview superview

这是关于网络浏览器。

我有一个处理网页的自定义类。

SNBrowserView
SNBrowserViewPage

SNBrowserViewPages有两个对象。

WKWebView
UIImageView // Snapshot of the WKWebView

一个功能可以维持或恢复用于内存管理的页面。

(测试)每当选择页面时,我都会调用恢复功能。

选择:

- (void)browserView:(SNBrowserView *)browserView didSelectPage:(SNBrowserViewPage *)page
{
        if (page.sustained) {

            [page recoverAnimated:NO];
        }
}

延:

- (void)sustain
{
    _sustained = YES;

    if (_webView) {

        _webView = nil;

        [_webView removeFromSuperview];
    }

    _snapshotView = [[UIImageView alloc] init];

    _snapshotView.frame = CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.height);

    _snapshotView.image = _snapshot;

    [self addSubview:_snapshotView];
}

恢复:

- (void)recoverAnimated:(BOOL)animated
{
    _sustained = NO;

    _webView = [[WKWebView alloc] init];

    _webView.frame = CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.height);

    _webView.navigationDelegate = self;

    _webView.UIDelegate = self;

    [self addSubview:_webView];

    [self sendSubviewToBack:_webView];

    [self loadURL:_initialURL]; // Start loading as early as possible.

    if (animated) {

        [UIView animateWithDuration:0.3
                         animations:^{

                             _snapshotView.alpha = 0.0;
                         }
                         completion:^(BOOL finished){

                             _snapshotView = nil;

                             [_snapshotView removeFromSuperview];
                         }];
    }
    else {

        _snapshotView = nil;

        [_snapshotView removeFromSuperview];
    }
}

当我尝试恢复页面时,snapshotView未设置为nil,也不会从superview中删除。

这怎么可能?

即使这样也不行:

- (void)recoverAnimated:(BOOL)animated
{
    _snapshotView = nil;

    [_snapshotView removeFromSuperview];
}

snapshotView是一个子视图,removeFromSuperview应该始终有效,为什么 MORE 呢?

1 个答案:

答案 0 :(得分:0)

我建议您尝试更换所有

_view = nil;

[_view removeFromSuperview];

[_view removeFromSuperview];

_view = nil;

因为您正在做的是将_view设置为nil,然后从超级视图中删除nil