2011-08-26 67 views
0

我正在使用彈出窗口視圖呈現大量您可以選擇的標誌。 我的代碼有點問題,因爲我打開這個popover內存後不久就沒有發佈(viewcontroller「flagsViewController」可以,乾淨,它會初始化並釋放它裏面的每一項內容。做錯了什麼?我怎樣才能儘快酥料餅被關閉釋放內存?發佈需要大量內存的popover

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

是你的應用程序崩潰?你有沒有崩潰報告或錯誤信息? –

+0

你是否使用popoverControllerDelegate方法? – MishieMoo

回答

2

與UIPopoverController工作已經很困難,但我做了以下的酥料餅控制器的代表設置自解決了這個問題( popoverController.delegate = self)並將UIPopoverControllerDelegate協議添加到您的類標題

接下來,我實現了- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController委託方法,在這裏我發佈了popoverController並將其設置爲nil。

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

請注意:如果您關閉通過代碼酥料餅此委託方法將不會被調用(即使用dismissPopoverAnimated),如果用戶通過酥料餅等

外攻駁斥它,它只會調用
+0

謝謝,這不是問題,但您的答案使我的代碼更加優雅和正確。謝謝。 – chewy