当我更改根视图控制器时,如何释放以前的视图控制器?

时间:2017-06-28 09:47:32

标签: ios objective-c

当我的应用开始启动时,第一个视图控制器为advertisement view controller,几秒后advertisement view controller将跳转到main view controller,我想将main view controller设置为root查看控制器。

以下是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];
}];
}

让我头疼的是:

跳转到main view controller后,dealloc advertisement view controller方法未被调用。

解决问题:

我在完成块中编写代码:

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

但是屏幕变黑,dealloc方法也没有被调用。

我想释放advertisement view controller的原因是我根本不会回到视图控制器。我想知道是否有任何方法可以释放视图控制器。

希望有人可以分享一个想法。非常感谢。

2 个答案:

答案 0 :(得分:1)

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

对于show Ad更改您的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];
}

广告完成后更改您的rootViewController。

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

答案 1 :(得分:0)

创建一个虚拟视图控制器作为RootViewController并将其分配给您的 AppDelegate中的NavigationVC

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

在视图中加载RootVC,添加广告VC

advertise = [[advert alloc] init];

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

[self.view addSubview: advertisement.view];

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

在1s后删除你的建议

[self.splashViewController.view removeFromSuperview];

self.splashViewController = nil;

推送你的主vc

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

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