简体   繁体   中英

How can I release the previous view controller when I change the root view controller?

When my app start launch,the first view controller is advertisement view controller ,after several seconds the advertisement view controller will jump to main view controller and I want to set the main view controller to root view controller.

Here is my code in the advertisement view controller :

- (void)gotoMainVC{
Xhany *mainVC = [[Xhany alloc] initWithNibName:nil bundle:nil];
UINavigationController *mainNaviController = [[UINavigationController alloc]initWithRootViewController:mainVC];
[self presentViewController:mainNaviController animated:NO completion:^{
    [[[[UIApplication sharedApplication] delegate] window] setRootViewController:mainNaviController];
}];
}

Which makes me have a headache is:

after jumping to main view controller the dealloc method of advertisement view controller didn't be invoked.

To solve the issue:

I write a code in completion block:

[self presentViewController:mainNaviController animated:NO completion:^{
    [[[[UIApplication sharedApplication] delegate] window] setRootViewController:mainNaviController];
    [self dismissViewControllerAnimated:NO completion:nil];
}];

But the screen become black and the dealloc method didn't be invoked,too.

The reason I want to release the advertisement view controller is I would not come back the view controller at all.I wonder if there any way to release the view controller.

Hope someone can share an idea.Thanks a lot.

In appDelegate

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

   if (isShowAd) { //Your Condition if You want to put or directly call prepareAd.
      [self prepareAd];
   }else{
      [self setNavRootViewController];
   }
   return YES;
}

For show Ad change your rootViewController.

- (void)prepareAd {

    AdvertismentViewController *theAdController = [[AdvertismentViewController alloc] init];
    self.window.rootViewController = theAdController;
    // Timer for Your ad.
    [NSTimer scheduledTimerWithTimeInterval:3 //Your time Interval for ad
                                     target:self
                                  selector:@selector(setNavRootViewController)
                                  userInfo:nil
                                   repeats:NO];
    [self.window makeKeyAndVisible];
}

When ad finished change Your rootViewController.

- (void)setNavRootViewController {
     Xhany *mainVC = [[Xhany alloc] initWithNibName:nil bundle:nil];
     UINavigationController *mainNaviController = [[UINavigationController alloc]initWithRootViewController:mainVC];
     self.window.rootViewController = mainNaviController;
     [self.window makeKeyAndVisible];
}

Create a dummy view controller as RootViewController and assign it to your NavigationVC in AppDelegate

self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];

self.window.backgroundColor = [UIColor whiteColor];

self.rootNavigationController = [[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]];

self.window.rootViewController = self.rootNavigationController;

[self.window makeKeyAndVisible];

In View Did Load of your RootVC,add advertisement VC

advertisement = [[advertisement alloc]init];

advertisement.view.frame = self.view.bounds;

[self.view addSubview: advertisement.view];

[self performSelector:@selector(removeSplashScreen) withObject:nil afterDelay:1];

Remove your advt after 1s

[self.splashViewController.view removeFromSuperview];

self.splashViewController = nil;

Push your main vc

MainVc *mainVc=[[MainVc alloc]init];

[self.navigationController pushViewController:mainVc animated:NO];

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