0

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.

oiledCode
  • 8,589
  • 6
  • 43
  • 59
  • Could it have been because viewWillDisappear: and viewDidDissappear: were not being called as explained in [this answer](http://stackoverflow.com/a/5279474/120497)? – djskinner Mar 30 '13 at 14:16

1 Answers1

1

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • I appreciate your answer but the solution you propose can't fit with my app architecture. To present a modal controller I have to wait for viewDidAppear – oiledCode Oct 16 '12 at 17:15
  • No, you can present a modal view controller from another view controller's viewDidLoad method. The user will never see the first controller. – rmaddy Oct 16 '12 at 17:23
  • Yes you are right, anyway this approach don't solve my memory management problem ,have just one view controller hierarchy at time – oiledCode Oct 16 '12 at 19:52