0

The initial view controller on my app is a UITabBarController that displays for logged in users.

For new users, however, my app delegate will point them to a login/registration view controller first:

// New user, show login
self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];

When the user has completed the login/registration, I then send the user to the tabbar as such:

// Login done, go to main view
[self performSegueWithIdentifier:@"userLoginToMainSeg" sender:self];

However, doing it this way, the LoginViewController is not released (dealloc is not called).

Can someone explain the error in my logic here?

Adam G
  • 1,188
  • 10
  • 24

1 Answers1

0

Your modal segue is basically doing:

[loginViewController presentViewController:mainViewController animated:YES completion: ...];

What this means is that mainViewController becomes loginViewControllers presentedViewController:

loginViewController.presentedViewController == mainViewController
mainViewController.presentingViewController == loginViewController

When you're presenting a view controller, the presenting view controller remains in the view controller hierarchy, so that you can later navigate back by calling:

[loginViewController dismissViewControllerAnimated: ...];

So it's perfectly normal that loginViewController is not released, since it's still the window's rootViewController. It's only that loginViewController is obstructed by the presented mainViewController.

If you want to eradicate loginViewController you can set window.rootViewController directly, but that wouldn't animate the transition. You can achieve animation by messing around the view controllers' views, but it's kind of outside the officially sanctioned territory...

IMO the cleanest solution would be to implement a basic container view controller that would be your window's rootViewController, and that could orchestrate the transition between loginViewController and mainViewController by animating their views, and then throwing away loginViewController. It would be kind of a primitive navigation controller without a navigation bar and a navigation stack – just swapping the current view controller with the new one, and throwing away the former.

Community
  • 1
  • 1
Tamás Zahola
  • 9,271
  • 4
  • 34
  • 46
  • thanks for the answer. Is there a way to release the loginviewcontroller after I present the tabbarcontroller? Would setting the tabbarcontroller as the rootviewcontroller do the trick? – Adam G Oct 08 '15 at 20:57
  • It would do, but wouldn't animate. I've updated the answer with some possible solutions. – Tamás Zahola Oct 08 '15 at 21:11