【问题标题】:Memory leak with UIModalTransitionStyleFlipHorizontalUIModalTransitionStyleFlipHorizo​​ntal 的内存泄漏
【发布时间】:2015-02-28 09:09:36
【问题描述】:

当以模态方式呈现视图控制器时,设置过渡样式似乎会造成内存泄漏。当下面的第三行没有被注释掉时,新的视图控制器没有响应,XCode 的 Debug Navigator 中的内存指示器稳步攀升,最终应用程序崩溃。当该行被注释掉时,它工作得很好。

- (IBAction)settingsPressed:(id)sender {
    SettingsPopupViewController *pvc=[[SettingsPopupViewController alloc] init];
    pvc.partyPlanName=[self partyPlanName];
    //pvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    pvc.modalPresentationStyle = UIModalPresentationFormSheet;
    pvc.delegate=self;
    [self presentViewController:pvc animated:YES completion:nil];
}

过渡样式之类的东西会导致这种情况似乎很奇怪,所以我的猜测是 UIModalTransitionStyleFlipHorizo​​ntal 的较长过渡时间可能会导致 SettingsPopupViewController 出现问题。也就是说,注释掉该对象中的所有 viewDidLoad 和 viewWillAppear 代码并没有任何区别。这种过渡风格是否存在任何已知问题?感谢阅读。

更新: 查看 Instruments 中的问题表明,当取消注释过渡行时,类别为“Malloc 272 字节”的行会稳步增加。

【问题讨论】:

  • ^(void){}????就说nil...好吧,更严肃点——使用仪器!它会准确地告诉您所有内存的去向以及原因。不用问,不用猜。

标签: ios memory-leaks


【解决方案1】:

这个问题是 iOS8 特有的,是由在 viewWillLayoutSubviews 中调整模态视图的大小引起的。通过添加 iOS 版本检查,问题得到解决:

- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0){
        self.view.superview.bounds = CGRectMake(0, 0, 366, 553);
    }
}

为了在 iOS8 设备上设置模态视图控制器的大小,我使用了调用对象中的以下代码(就在调用 presentViewController 之前):

modalViewController.preferredContentSize = CGSizeMake(366, 553);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-30
    • 2013-09-17
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    相关资源
    最近更新 更多