Pregunta

I'm facing a strange problem.. I want to add a UIViewcontroller (called iView) to my current View. I do it by calling

   iView = [[KFKaraokeInfosView alloc] initWithKaraoke:karaoke NibName:@"InfosView" commingFromPlayer:NO];
    iView.songTitle.text = karaoke.title;
    [self.view addSubview:iView.view];

In the viewDidLoad of iView, I add it as an observer to the NotificationCenter for a certain notification, like this

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        self.title = @"About";

        if ([karaoke.styles count] == 0)
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"GetInfosOfSong" object:self.karaoke];
        }
        else
        {
            shouldSetup = YES;
        }

        [self.navigationController setNavigationBarHidden:NO animated:YES];
        [self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setup) name:GetSongInfos  object:nil];
        [optionsTableView setBackgroundView:nil];

    }

The problem is when I call autorelease method on iView at the initialisation, the notification is never catched (so setup method is never called), but if I don't call autorelease for iView, it works.

I don't understand the memory management in this situation, can someone help me to understand ?

¿Fue útil?

Solución

The container view controller methods are found in Implementing a Container View Controller of the UIViewController Class Reference, and sample code can be found in Creating Custom Container View Controllers of the View Controller Programming Guide.

Thus, in iOS 5 and later, you should probably have:

iView = [[[KFKaraokeInfosView alloc] initWithKaraoke:karaoke NibName:@"InfosView" commingFromPlayer:NO] autorelease];
[self addChildViewController:iView];
iView.songTitle.text = karaoke.title;
[self.view addSubview:iView.view];
[iView didMoveToParentViewController:self];

If this is iOS 4 or earlier, it doesn't support proper containment. You can manually kludge it, by adding the view like you are, with no autorelease:

iView = [[KFKaraokeInfosView alloc] initWithKaraoke:karaoke NibName:@"InfosView" commingFromPlayer:NO];
iView.songTitle.text = karaoke.title;
[self.view addSubview:iView.view];

You'd obviously keep a copy of the child view controller in some ivar of the parent view controller, not autorelease it, but rather explicitly release the child's controller when the child's view is dismissed. Note, since you're operating in a pre-containment iOS4 world, your child controller is not guaranteed of receiving various events (notoriously rotation events). But you should receive your notification center events.


An alternative to this ugliness of trying to fake containment in iOS 4 is to not use a child view controller at all, but just add the child view to the parent view. You can actually add it to the parent controller's NIB, but just hide it. Then, when you want to present it, unhide it. But keep everything in the parent view controller and it's NIB. The method that I described above, faking containment, might work (I've seen people do it), but it gives me the heebie jeebies. This is simpler.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top