dismissViewControllerAnimated:completion:保留以前的视图控制器吗?

时间:2012-06-28 16:29:54

标签: ios uiviewcontroller uistoryboard

我的应用程序中有内存泄漏和崩溃,我想知道它们是否可能是由于我的容器View Controller在View Controllers之间切换的方式。应用程序应允许用户导航一长串页面。每个页面都作为Storyboard中的ViewController布局(每个页面都有一个数字作为标识符)。

当我到应用程序的第14页时,我可以在Instruments的Activity Monitor中看到该应用程序需要大约600MB的内存(在iPad 3上)。这是因为每个视图控制器都有UIImageViews和大图像。

我正在使用ARC。

以下是容器View Controller的代码。你能在某处看到内存管理问题吗?

@implementation PageNavigator

int startingPage = 0;
int currentPage = 0;

-(void)viewDidAppear:(BOOL)animated{
    NSLog(@"Starting book from page %d", startingPage);

    //do this only the first time the app runs
    if(startingPage != -1){
        UIViewController *currentPageVC = [self.storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:@"%d", startingPage]];
        [self presentModalViewController:currentPageVC animated:YES];
        currentPage = startingPage;
        startingPage = -1;
    }
}

//currentPageVC and its outlets should get released when it gets out of scope, right? 
-(IBAction)goToNextPage{
    [self dismissViewControllerAnimated:YES completion:^{

        currentPage++;
        UIViewController *currentPageVC = [self.storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:@"%d", currentPage ]];
        [self presentModalViewController:currentPageVC animated:YES];

        NSLog(@"Current Page is %d", currentPage); 
    }];
}

1 个答案:

答案 0 :(得分:0)

当调用dismissViewControllerAnimated时,控制转到该控制器的dealloc方法,如果你没有使用ARC并为某些UI控件分配内存,那么你需要释放它们,并且你也不需要这样做照顾控制器的记忆。