【问题标题】:What is the most correct way to add a UIViewController to a subview and remove it?将 UIViewController 添加到子视图并删除它的最正确方法是什么?
【发布时间】:2012-04-30 04:29:13
【问题描述】:

我正在尝试添加 UIViewController 子视图,然后单击按钮将其关闭。我当前的代码可以完成这项工作,我只是不确定它是否会泄漏或导致任何问题。

所以我首先添加子视图

-(IBAction)openNewView:(id)sender{
   // start animation
   [UIView beginAnimations:@"CurlUp" context:nil];
   [UIView setAnimationDuration:.3];
   [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
   [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];

   // add the view
   newViewController* newVC = [[newViewController alloc] initWithNibName:@"newViewController" bundle:[NSBundle mainBundle]];
   [self.view addSubview:newVC.view];

   [UIView commitAnimations]; 
}

然后在 newViewController.m 我有删除它的功能

-(IBAction)closeNewView:(id)sender{
   // start animation
   [UIView beginAnimations:@"curldown" context:nil];
   [UIView setAnimationDelegate:self];
   [UIView setAnimationDuration:.3];
   [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
   [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view.superview cache:YES];

   // close dialog
   [self.view removeFromSuperview];
   [UIView commitAnimations];

   [self.view release];
}

就像我说的那样有效,但是当我分析代码时,它告诉我:

在第 X 行分配并存储到“newViewController”中的对象的潜在泄漏:

newViewController* newVC = [[newViewController alloc] initWithNibName:@"newViewController" bundle:[NSBundle mainBundle]];

和

[self.view release]; 的调用者此时不拥有的对象的引用计数的递减不正确

如果我 autorelease viewController 而不是 [self.view release] 它在删除时崩溃(如果我在添加视图后释放视图):-[FirstViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0xd21c7e0

如果我在任一 viewControllers dealloc 中调用 [newVC release],则构建失败。

希望我不是在问一个相当明显的问题,但是添加和删除 viewControllers 的正确方法是什么?

【问题讨论】:

    标签: ios uiviewcontroller release-management addsubview memory-management


    【解决方案1】:

    您使用的是哪个 IOS 版本?如果您使用的是 5.0,您可能应该继续切换到 ARC,这样您就不必自己处理 [release] 调用。

    如果您仍然想要或需要坚持手动内存管理:如果您尝试释放 newVC,dealloc 无法构建的原因是因为该指针的作用域是函数 openNewView。将其设为班级成员,您就可以将其释放。

    @implementation WhateverItsCalled {
      newViewController *newVC;
    }
    
    - ( IBAction ) openNewView: (id)sender {
    ...
      newVC = [ [ newViewController alloc ] initWithNibNamed:...
    }
    
    - ( void ) dealloc {
      [ newVC release ];
    }
    

    是的,如果您不使用 ARC,则每个“alloc”必须与相应的“release”配对。

    另外我必须问 - 你为什么在这里使用视图控制器?如果您只想要一个视图,您可以使用 [ [ NSBundle mainBundle ] loadNibNamed] 从 NIB 加载视图,并将 [self] 列为文件的所有者。这将设置您的所有引用(包括您想要的视图),并且您不必实例化(看起来像)一个多余的视图控制器。

    【讨论】:

    • 这真的是我的第二个应用程序,也是第一个有多个视图的应用程序,所以我对此很陌生。我想我没想过只用一个笔尖。无论如何,我在我的构建设置中打开了 ARC,清理了所有版本以及其他什么,它出现了[newViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0x6b34c70。如果我将其更改为@interface newViewController : UIView { 并调用[[NSBundle mainBundle] loadNibNamed:@"dateViewController" owner:self options:nil];(这对我来说可能是错误的),它会立即在 Main.m 中的 SIGABRT
    • 您从哪里得到“发送到已释放实例的消息”错误?设置异常断点(Cmd+6 显示断点视图,左下角的“+”)以查看它的确切来源。
    • 最有可能的是,您获得 SIGABRT 是因为您的 NIB 中命名的插座在“self”中没有相应的属性。转到NIB,单击“文件所有者”占位符,然后切换到出口视图(Cmd+Option+6)。加载NIB时,其中命名的每个变量都必须在“self”中具有相应的属性。 (您可能在视图控制器本身中有类似的属性)。
    • 我添加了异常断点,但是它从来没有出现“消息发送到已释放实例”错误。然而,当我在[[NSBundle mainBundle] loadNibNamed:@"dateViewController" owner:self options:nil];line 上切换回 UIView 时,它确实出现了,即使在我检查了插座视图之后也是如此。它还在控制台中出现警告。警告:尝试使用不在框架中的块创建 USE_BLOCK_IN_FRAME 变量。
    猜你喜欢
    • 2011-10-26
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多