无法删除子视图?

Can't remove subview?

这是关于网络浏览器的。

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

SNBrowserView
SNBrowserViewPage

SNBrowserViewPages 有两个对象。

WKWebView
UIImageView // Snapshot of the WKWebView

函数维持或恢复内存管理页面。

(Testing) Whenever a page is selected I call a recovery function.

选择:

- (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

我建议你尝试更换所有

_view = nil;

[_view removeFromSuperview];

[_view removeFromSuperview];

_view = nil;

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