简体   繁体   English

无法删除子视图?

[英]Can't remove subview?

This is about a web browser. 这是关于网络浏览器的。

I have a custom Class that handles the webpages. 我有一个处理网页的自定义类。

SNBrowserView
SNBrowserViewPage

SNBrowserViewPages have two objects. SNBrowserViewPages有两个对象。

WKWebView
UIImageView // Snapshot of the WKWebView

A function either sustains or recovers a page for memory management. 一个功能可以维持或恢复页面以进行内存管理。

(Testing) Whenever a page is selected I call a recovery function. (测试)无论何时选择页面,我都会调用恢复功能。

Selection: 选择:

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

            [page recoverAnimated:NO];
        }
}

Sustain: 支持:

- (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];
}

Recover: 恢复:

- (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];
    }
}

When I try to recover a page the snapshotView is not set to nil nor is it removed from the superview. 当我尝试恢复页面时,snapshotView不会设置为nil,也不会从超级视图中删除。

How is that even possible? 这怎么可能呢?

Even this won't work: 即使这样也行不通:

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

    [_snapshotView removeFromSuperview];
}

The snapshotView is a subview, removeFromSuperview should always work, why is there MORE to it? snapshotView是一个子视图,removeFromSuperview应该总是可以工作,为什么还有更多呢?

I suggest you try replace all of your 我建议您尝试替换所有

_view = nil;

[_view removeFromSuperview];

with

[_view removeFromSuperview];

_view = nil;

because what you are doing is setting the _view to nil and then removing nil from superview. 因为您正在做的是将_view设置为nil ,然后从_view视图中删除nil

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM