释放占用大量内存的popover

时间:2011-08-26 15:27:22

标签: ios objective-c memory-management popover

我正在使用弹出窗口来显示您可以选择的大量标记。 我的代码有问题,因为我打开这个popover内存后不久就没有释放(viewcontroller“flagsViewController”是好的和干净的,它会初始化并释放它内部的每一个项目。

我做错了什么?弹出窗口关闭后如何释放内存?

    -(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];
}

1 个答案:

答案 0 :(得分:2)

使用UIPopoverController非常困难,但我通过执行以下操作解决了这个问题:将Popover控制器的委托设置为self(popoverController.delegate = self)并将UIPopoverControllerDelegate协议添加到类标题

接下来,我实现了- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController委托方法,在这里我发布了popoverController并将其设置为nil。

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

请注意:如果您通过代码解除popover(即使用dismissPopoverAnimated),则不会调用此委托方法,只有当此用户通过点击popover等外部解除它时才会调用它。