0

the key window's rootViewController is a login ViewController。In the login viewController, i will modal or push other viewControllers. When login successfully, it would switch the rootViewController to viewController C. I find the login viewController's dealloc method is not fired.

I found same question similarity my question, but not resolve my question.

The test demo code:

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

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
ViewControllerA *vc =  [[ViewControllerA alloc] init];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];

return YES;
}

In the viewControllerA(vcA), i modal viewControllerB(vcB)

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

ViewControllerB *vcB = [[ViewControllerB alloc] init];
[self presentViewController:vcB animated:YES completion: nil];

}

In the vcB, i switch the key window's rootViewController to vcC

UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
//        [keyWindow.rootViewController removeFromParentViewController]; 
//        keyWindow.rootViewController = nil; 
keyWindow.rootViewController = [ViewControllerC new];

you will find, the dealloc method of vcA will not fire.

If i dismiss the vcB when switch the rootViewController, it would be good

[self dismissViewControllerAnimated:NO completion: nil];

UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
//        [keyWindow.rootViewController removeFromParentViewController]; // 无效
//        keyWindow.rootViewController = nil; // 无效
keyWindow.rootViewController = [ViewControllerC new];

so, what should i do to dealloc vcA?

Here is my test demo enter link description here

ming
  • 1
  • 2
  • Could you show us your whole implementation of vcA ? – Randy Nov 30 '16 at 08:57
  • @Randy in the test demo , the vcA just modal vcB, like: `[self presentViewController:vcB animated:YES completion: nil];` – ming Nov 30 '16 at 13:54
  • @Randy you can see my test demo in my github [link](https://github.com/againxu/test-demo.git) – ming Nov 30 '16 at 14:20
  • Have you taken a look in Instruments, to see if there is a hanging reference? – Stephen Nov 30 '16 at 14:39
  • @Stephen I used leaks tool to view, but found no leaks – ming Dec 01 '16 at 01:39
  • @Stephen maybe you would like to run my demo, here is the link [link](https://github.com/againxu/test-demo.git) – ming Dec 01 '16 at 01:41
  • I believe the best practice here would be to dismiss the viewcontrollers until you hit the root, then switch the rootviewcontroller out. I did some testing with your project and found that works (like in your example above, and the commented out line in your test). I believe the reason is that vcA has a strong reference to vcB and vcB has one to vcA, as part of managing the view hierarchy. – Stephen Dec 02 '16 at 02:35
  • @Stephen yes, it would be good to dismiss the viewcontrollers. But controller a modal controller b, b modal c, in turn go on a number of times, it would be trouble to dismiss controllers. How could i switch the rootviewcontroller friendly? I want to get the other method to do it. It's very appreciable – ming Dec 02 '16 at 07:00

0 Answers0