0

In the app im creating there are many pages that look mostly the same with some part which is different. To handle this kind of situation i created a container controller that contains a subview. I want this subview to be filled by the contents of another controller (and its associated nib) which i will created dynamically as needed based on context.

I have the following method somewhere

- (void) someAction {
    UIViewController* contentController = [[MyContentController alloc] init];
    UIViewController* containerController = [[MyContainerController alloc] initWithContentController:contentController];
    [navigationController pushViewController:pageController animated:YES];
    [contentController release];
    [containerController release];
}

In MyContainerController.m i retain the controller in a property

- (id)initWithContentController:(UIViewController *)aContentController {
    if ((self = [super initWithNibName:@"MyContainerController" bundle:nil])) {
        contentController = aContentController;
    }
    return self;
}

Later in viewDidLoad i do the following

- (void)viewDidLoad {
    [super viewDidLoad];
    [contentViewContainer addSubview:contentController.view];
}

contentViewContainer is the view that's supposed to hold the page specific info. Unfortunatly this fails with EXC_BAD_ACCESS. The funny thing is that if i alloc and init the content controller from within viewDidLoad everything works. It seems that i cant pass a contoller i allocated from another place. Can anyone assist.

nsof
  • 2,299
  • 1
  • 20
  • 28

1 Answers1

0

Since you are releasing contentController in the actionMethod you have to retain contentController in you init method

- (id)initWithContentController:(UIViewController *)aContentController {
    if ((self = [super initWithNibName:@"MyContainerController" bundle:nil])) {

        contentController = [aContentController retain];
    }
    return self;
}

But, why do you need this? Controllers are supposed to control views and no other controllers. If you think you really need that then you want to use UINavigationController or UITabBarController maybe. You can also load views without a controller (see here)

I personally think that having UIViewControllers inside of simple UIViewController is not a preferable approach

Hope it helps

Community
  • 1
  • 1
nacho4d
  • 43,720
  • 45
  • 157
  • 240
  • Thanks for responding. First, you are right that this is similar to toolbar/nav controllers. Its exactly the same idea but they are not a good fit for my needs. Second, isnt this exactly the purpose of having contentController as a property with retain (so that i would not need to retain it myself)? – nsof Dec 11 '10 at 09:26
  • If you are aware of them (UINavigationController or UITabBarController) and still don't think they are what you want them is Ok. Second, Yes is like having a property with retain. If so you need to release it in dealloc implementation. You could also have it as assign but you have to ensure the contained controller is not released or you app will crash again (EXC_BAD_ACCESS). In this case is better to retain ;) – nacho4d Dec 11 '10 at 09:59