简体   繁体   中英

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.

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.

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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