Ios 更改根视图控制器时,如何释放上一个视图控制器?

Ios 更改根视图控制器时,如何释放上一个视图控制器?,ios,objective-c,Ios,Objective C,当我的应用启动时,第一个视图控制器是广告视图控制器,几秒钟后广告视图控制器将跳转到主视图控制器,我想将主视图控制器设置为根视图控制器 - (void)setNavRootViewController { Xhany *mainVC = [[Xhany alloc] initWithNibName:nil bundle:nil]; UINavigationController *mainNaviController = [[UINavigationController allo

当我的应用启动时,第一个视图控制器是广告视图控制器,几秒钟后广告视图控制器将跳转到主视图控制器,我想将主视图控制器设置为根视图控制器

- (void)setNavRootViewController {
     Xhany *mainVC = [[Xhany alloc] initWithNibName:nil bundle:nil];
     UINavigationController *mainNaviController = [[UINavigationController alloc]initWithRootViewController:mainVC];
     self.window.rootViewController = mainNaviController;
     [self.window makeKeyAndVisible];
}
以下是我在广告视图控制器中的代码:

让我头疼的是:

跳转到主视图控制器后,未调用广告视图控制器的dealloc方法

要解决这个问题:

我在完成块中编写代码:

[self presentViewController:mainNaviController animated:NO completion:^{
    [[[[UIApplication sharedApplication] delegate] window] setRootViewController:mainNaviController];
    [self dismissViewControllerAnimated:NO completion:nil];
}];
但是屏幕变黑,dealloc方法也没有被调用

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

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

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

创建一个虚拟视图控制器作为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

广告=[[advision alloc]init]

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

[self.view addSubview: advertisement.view];

[self performSelector:@selector(removeSplashScreen) withObject:nil afterDelay:1];
1s后移除advt

[self.splashViewController.view removeFromSuperview];

self.splashViewController = nil;
推动你的主要风险投资

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

[self.navigationController pushViewController:mainVc animated:NO];
在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];
}
ad完成后,更改您的rootViewController

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

检查是否有任何强IB插座导致此情况。使其变弱,以便在广告视图控制器中调用dealloc方法。从显示广告视图的位置controller@KKRocks我没有使用任何iboutlet,也没有使用任何xib或故事板。@Appdelegate.m中的vp2698。代码为:```@property-strong,非原子UINavigationController*navCon;-voidgotoAdsVC{ADViewController*adVC=[[ADViewController alloc]init];self.navCon=[[UINavigationController alloc]initWithRootViewController:adVC];self.window.rootViewController=self.navCon;}``是的,你是对的,但是,如果我使用真实视图控制器,没有办法吗?不,应该有一个视图控制器分配给导航控制器。如果您直接分配advt。在VC上执行pop操作时会显示VC黑屏。