1

I have a LoginViewController (UIViewController) that when all the criteria is met and the user hits the Login button, a storyboard segue is run that pushes the ProfileViewController (UIViewController). When this happens, I have a log statement in my LoginViewController's dealloc method to see if it is called and to my disappointment it is never called. My question is whether or not it is supposed to be called? Also, when I log in, sometimes I get a "Received memory warning" and sometimes I do not which I find strange because I am taking the exact same steps in both cases and yet i get a memory warning one time and not with the other.

Anyone can shine some light on this that would be great!

Thanks.

Mike Simz
  • 3,976
  • 4
  • 26
  • 43

2 Answers2

2

UINavigationController maintains a stack of view controllers. You start with one element, a LoginViewController, on that stack. When you push a ProfileViewController, you now have two elements on the stack. The LoginViewController can't be deallocated until it is removed from the stack.

If you want the ProfileViewController to replace the LoginViewController on the navigation controller's stack, you can write a custom segue class to implement that behavior. See this Q&A.

(You might think you could use the “Replace” or “Show Detail (e.g. Replace)” segue type in your storyboard, but those only work if you are using a UISplitViewController.)

Community
  • 1
  • 1
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • Thanks for the response..I also have a Logout feature in my ProfileViewController. When you click the Logout button you are returned to the LoginViewController, and this is done via an UnwindSegue. If I were to replace the LoginViewController with the ProfileViewController instead of adding it to the stack would the Logout feature still work as expected or will it cause a crash? @rob mayoff – Mike Simz Sep 18 '14 at 18:53
  • If you're using an unwind segue to return to the `LoginViewController`, you need to leave the `LoginViewController` on the stack, and you want it to stay allocated. It doesn't sound like you actually have a problem. – rob mayoff Sep 18 '14 at 19:05
  • My app runs completely fine. But when I see a 'Received Memory Warning' 5 seconds after logging in and getting to the ProfileViewController, I begin to think I do have a problem @rob mayoff – Mike Simz Sep 18 '14 at 19:07
  • If you think you have a memory problem, run your app under the Allocations instrument. (Menu bar > Product > Profile to get started.) – rob mayoff Sep 18 '14 at 19:13
0

With ARC enabled, when a object is not referenced, it will be released.

In order to display view from ProfileViewController, you instantiate a object of it in LoginViewController, and that's how you still can see the profile view after it is presented. If LoginViewController instance is released, the profile view will also get released(assume no one else references it). For the same reason, the LoginViewController instance is not released because another object is holding a reference to it. Say your views are presented in Window -> ProfileViewController -> ProfileViewController, it's the window that keeps ProfileViewController instance from being released.

If you have two views as I assumed so far, the memory warning should be from somewhere else. Two views cannot cause the issue.

Dino Tw
  • 3,167
  • 4
  • 34
  • 48