ios - How to release the previous view controller after switching the root view controller?
扔个三星炸死你
扔个三星炸死你 2017-07-01 09:12:25
0
2
1368

For example: the first VC entered after starting the APP is the advertising VC. The root view controller at this time is the advertising VC. After a few seconds, it jumps to the homepage VC. At this time, I switch the root view controller to the homepage. VC. Since I no longer need to go back to the advertising VC, I want to release the advertising VC. Is there any way?
Students who have solved this problem will share it.

扔个三星炸死你
扔个三星炸死你

reply all(2)
给我你的怀抱

I tested this situation at the earliest. Such advertising VC will not be released automatically, which means that it always takes up memory, but it is quite small.

You can find the pointer variable of the advertising vc and set it to nil. This operation should be performed after the next interface appears to avoid sudden changes in the view

Another alternative is to manually clear the control pointer in the life cycle method viewDidDisappear of the ad vc, such as self.view = nil, the UIImageView control of the ad, and because the ad only needs to be loaded once and is large Picture, it is best to use the contentsOfFile method

代言

Directly modify the rootController of Window to release the previous advertising VC.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _window.backgroundColor = [UIColor whiteColor];

    _window.rootViewController = [ADViewController new];

    [_window makeKeyAndVisible];
    
    // 2.0秒后跳转到mainController
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

    MainViewController *VC = [MainViewController new];
    
    // 修改 rootViewController 后, ADViewController会释放
    _window.rootViewController = VC; 
    });

    return YES;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template