【问题标题】:Does dismissViewControllerAnimated:completion: retain previous view controllers?dismissViewControllerAnimated:completion: 是否保留以前的视图控制器?
【发布时间】:2012-06-28 16:29:54
【问题描述】:

我的应用程序出现内存泄漏和崩溃,我想知道它们是否是由于我的容器视图控制器在视图控制器之间切换的方式造成的。该应用程序应允许用户浏览一长串页面。每个页面都在 Storyboard 中作为 ViewController 布局(每个页面都有一个数字作为其标识符)。

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

我正在使用 ARC。

下面是容器视图控制器的代码。你能在某处看到内存管理问题吗?

@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); 
    }];
}

【问题讨论】:

    标签: ios uiviewcontroller uistoryboard


    【解决方案1】:

    当dismissViewControllerAnimated 被调用时,控制转到该控制器的dealloc 方法,如果您不使用ARC 并将内存分配给某些UI 控件,那么您需要释放它们,而且您也不需要注意视图控制器的内存。

    【讨论】:

    • 正如我在问题中提到的,我正在使用 ARC。
    • @Eric - 抱歉,最后提到了所以我错过了。你可以检查在 dismissViewControllerAnimated 时是否调用了 dealloc 方法,所以如果你做了一些手动内存处理,你可以在那里处理。
    猜你喜欢
    • 2012-04-23
    • 1970-01-01
    • 2017-04-23
    • 2011-07-26
    • 2011-05-27
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多