Home  >  Q&A  >  body text

ios - How to release the previous view controller after switching the root view controller?

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.

扔个三星炸死你扔个三星炸死你2690 days ago1347

reply all(2)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-07-01 09:14:25

    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

    reply
    0
  • 代言

    代言2017-07-01 09:14:25

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

    reply
    0
  • Cancelreply