Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the rootViewController of a UIWindow

Tags:

When my app first loads, I set the rootViewController property of my UIWindow to controllerA.

Sometime during my app, I choose to change the rootViewController to controllerB.

The issue is that sometimes when I do a flip transition in controllerB, I see controllerA's view behind it. For some reason that view isn't getting removed. Whats even more worrying is that after setting the rootViewController to controllerB, controllerA's dealloc method never gets fired.

I've tried removing the subviews of UIWindow manually before switching to controllerB, that solves the issue of seeing controllerA's views in the background but controllerA's dealloc still never gets called. Whats going on here????

Apples docs say:

The root view controller provides the content view of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller’s view as the content view of the window. If the window has an existing view hierarchy, the old views are removed before the new ones are installed.

UPDATE

Here's the code of my AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];     [self showControllerA];     [self.window makeKeyAndVisible];     return YES; }  - (void)showControllerA {     ControllerA* a = [ControllerA new];     self.window.rootViewController = a; }  - (void) showControllerB {     ControllerB* b = [ControllerB new];     self.window.rootViewController = b; } 
like image 411
aloo Avatar asked Jul 13 '13 16:07

aloo


People also ask

What is rootViewController in iOS?

The root view controller provides the content view of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller's view as the content view of the window.

How do I get rootViewController of UINavigationController?

The root view controller is simply the view controller that sits at the bottom of the navigation stack. You can access the navigation controller's array of view controllers through its viewControllers property. To access the root view controller, we ask for the first item of the array of view controllers.


1 Answers

It turns out there are two separate issues. 1) I had a retain cycle in Controller A so it was never getting dealloc'd. Secondly, in order to change the root view controller you must remove the windows subviews first (even though the docs suggest otherwise)

like image 182
aloo Avatar answered Sep 28 '22 20:09

aloo