Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8981105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:15:03+00:00 2026-06-15T20:15:03+00:00

I have a custom container UIViewController that has six child UIViewControllers, and a set

  • 0

I have a custom container UIViewController that has six child UIViewControllers, and a set of tabs that the user interacts with to switch between the child view controllers. The problem is when my container view controller is released the child view controllers are not.

I have verified that the child view controllers are not released by adding some debugging code to their dealloc methods, and they are released as long as their view’s are not added to the container view controller’s view.

Below is an a excerpt of the code I’m using to create my custom container view controller. The viewController pointers are iVars. I am also using ARC so that is why there are no actual release calls.

- (void)init 
{
    if ((self = [super init])) { 
        vc1 = [[UIViewController alloc] init];
        [self addChildViewController:vc1];

        vc2 = [[UIViewController alloc] init];
        [self addChildViewController:vc2];

        vc3 = [[UIViewController alloc] init];
        [self addChildViewController:vc3];

        vc4 = [[UIViewController alloc] init];
        [self addChildViewController:vc4];

        vc5 = [[UIViewController alloc] init];
        [self addChildViewController:vc5];

        vc6 = [[UIViewController alloc] init];
        [self addChildViewController:vc6];
    }
    return self;
}

- (void)dealloc
{
    [vc1 removeFromParentViewController];
    vc1 = nil;

    [vc2 removeFromParentViewController];
    vc2 = nil;

    [vc3 removeFromParentViewController];
    vc3 = nil;

    [vc4 removeFromParentViewController];
    vc4 = nil;

    [vc5 removeFromParentViewController];
    vc5 = nil;

    [vc6 removeFromParentViewController];
    vc6 = nil;
}

- (void)switchFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController
{
    if (fromViewController) {
        [fromViewController.view removeFromSuperview];
    }

    [self.view addSubview:toViewController];
    toViewController.view.frame = self.view.bounds;
}

Do you all have any ideas what I’m doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:15:04+00:00Added an answer on June 15, 2026 at 8:15 pm

    As I suspected, the problem is not related to the view controller containment code in the question, but rather your adding of the observers (which you discuss in your answer to this question):

    [[NSNotificationCenter defaultCenter] addObserverForName:kUpdateEventName object:nil queue:nil usingBlock:^(NSNotification *note) {
        // do stuff when update happen
    }];
    

    And that you tried to remove it with

    [[NSNotificationCenter defaultCenter] removeObserver:self name:kUpdateEventName object:nil];
    

    So, there are two problems:

    1. If you use addObserverForName:object:queue:, this is not the correct way to remove this observer. Instead, define a property to keep track of the observer:

      @property (nonatomic, weak) id<NSObject> notification;
      

      Then save a reference to that observer when you create it:

      self.notification = [[NSNotificationCenter defaultCenter] addObserverForName:kUpdateEventName object:nil queue:nil usingBlock:^(NSNotification *note) {
          // do something
      }];
      

      And when you want to remove it, use this reference:

      [[NSNotificationCenter defaultCenter] removeObserver:self.notification];
      

      That would ensure that the observer would be removed properly.

    2. The failure of the child view controllers to be released while this observer was in place means that this block that you passed to addObserverForName:object:queue: must of had a reference to self. If you tried to remove this observer correctly (as shown above) in dealloc, you would still have a strong reference cycle (formerly known as a retain cycle). This is resolved in a number of ways, but the most robust pattern is to prevent the strong reference cycle in the first place by using the weakSelf pattern:

      typeof(self) __weak weakSelf = self;
      
      self.notification = [[NSNotificationCenter defaultCenter] addObserverForName:kFooNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
          // use `weakSelf` in this block; not `self`
      }];
      

    My original answer is below:


    While Srikanth is right that after addChildViewController, you should call didMoveToParentViewController:self and before removeFromParentViewController you should call willMoveToParentViewController:nil. But that’s not your problem. In fact, I used a variation of your code (even without the dealloc), and the children controllers are released fine.

    Bottom line, I suspect your problem rests elsewhere, probably a retain cycle somewhere. For example, do you children have strong reference to the parent? Are you using recurring timers? You reference some tabs. You’re not using a tab bar controller, are you? It’s got to be something like that.

    [Refer to revision history if you want to see rest of the original answer with code snippets and minor details on the OP’s code sample]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a test app that uses a custom container controller to switch between
I have a Custom Control that has multiple textbox fields and a checkbox contained
In my iPhone app I have a UIViewController that has a UITableView and another
We have a custom canvas which has specialized nodes that behave a lot like
I have a custom UIViewController class that contains my similar methods and UI components
I have custom UIView that has a xib file as well as .h and
I have a custom type instance that needs to be registered in code: container.RegisterType(
I have a custom form that has 4 panels on it edges. I would
I have a custom control for a LayoutPanel that holds child controls. I want
I have a custom container folder. I named it .llp . It is actually

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.