Search code examples
iosobjective-cmemory-managementpopover

Releasing a popover which takes great amount of memory


I am using a popover view to present a large amount of flags of which the your can select. There is something wrong with my code since soon after I open this popover memory is not released (the viewcontroller "flagsViewController" is ok and clean, it does init and release each and every item inside of it.

What am I doing wrong? How can I free memory as soon as the popover is closed?

    -(void)presentFlags
    {
        [self.popoverController dismissPopoverAnimated:YES];

        FlagsViewController *controller = [[FlagsViewController alloc] 
                                            initWithNibName:@"FlagsViewController" 
                                            bundle:[NSBundle mainBundle]] ;

        UINavigationController *container = [[UINavigationController alloc] initWithRootViewController:controller];

        UISegmentedControl *ctrl = [[UISegmentedControl alloc] initWithItems:segmentedItems];
        ctrl.frame = CGRectMake(0, 6, 500, 30);
        [ctrl addTarget:self action:@selector(changeSeg:) forControlEvents:UIControlEventValueChanged];
        ctrl.segmentedControlStyle = UISegmentedControlStyleBar;
        //ctrl.momentary = YES; 
        ctrl.tintColor  = [UIColor darkGrayColor];
        UIImage *theImage = [UIImage imageNamed:@"highlight_country.png"];

        [ctrl setImage:theImage forSegmentAtIndex:0];

        [container.navigationBar addSubview:ctrl];
        [ctrl release];
        //
        //create a popover controller
        self.popoverController = [[[UIPopoverController alloc]
                                   initWithContentViewController:container] autorelease];
        [container  release];
        [popoverController setPopoverContentSize:CGSizeMake(500, 600)];
        //present the popover view non-modal with a
        //refrence to the button pressed within the current view
        [popoverController presentPopoverFromRect:CGRectMake(popoverArrowPossition, 0.0, 0.0, 52.0) inView:super.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
        [controller release];
}

Solution

  • Working with the UIPopoverController has been pretty difficult but I solved this problem by doing the following setting the Delegate of the Popover Controller to self (popoverController.delegate = self) and adding the UIPopoverControllerDelegate Protocol to your Class Header

    Next, I implemented the - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController delegate method and here I released the popoverController and set it to nil.

    - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
        [self.popoverController release];
        self.popoverController = nil;
    }
    

    Please note: This delegate method won't be called if you dismiss the popover via code (i.e. using dismissPopoverAnimated), it'll only be called if this User dismisses it by tapping outside the popover etc.