I’m facing the following problem : My app has two main controller (a)loginController and (b) contentController, when the app is launched I check if the user is logged in if yes I show the contentController otherwise I show the login controller. So basically in didFinishLaunchingWithOptions I assign one of this controller to window.rootViewController. The problem is when I want to switch from one controller to the other (because the user made a login or logout) to accomplish this I use the following code :
[UIView transitionWithView:self.window
duration:0.65
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
self.window.rootViewController = self.contentController;
}
completion:^(BOOL finished){
[self.loginController release];
}];
before this transition window.rootViewController was loginController, the problem here is that when this code is executed I receive the following error:
-[loginController _preferredInterfaceOrientationGivenCurrentOrientation:]: message sent to deallocated instance 0x1c55b490
I would like to understand how can I release my controller without getting this error.
It would be also great if someone could suggest me what is the best approach to change window.rootViewController at runtime.
Without seeing a lot more code it is impossible to determine why you are having memory management issues. But I’d like to offer a different answer. Make your content controller the window’s root controller at all times. If you need to show the login screen, present it as a modal view controller over the content controller. This will be much easier than switching root view controllers. You can present it with no animation on startup so the user never sees it transition. Upon login you could dismiss any number of ways to reveal the content controller underneath it. If the user logs out, you can then present the login controller again, as a modal controller over the content controller.