0

我正在使用一个弹出视图来展示您可以选择的大量标志。我的代码有问题,因为在我打开此弹出窗口后不久内存没有释放(视图控制器“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];
}
4

1 回答 1

2

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

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

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

请注意:如果您通过代码关闭弹出框(即使用dismissPopoverAnimated),则不会调用此委托方法,仅当该用户通过在弹出框外点击等方式关闭它时才会调用此委托方法。

于 2011-08-26T15:47:32.440 回答