【问题标题】:Does releasing a view controller releases all its properties?释放视图控制器是否会释放其所有属性?
【发布时间】:2012-03-15 02:55:35
【问题描述】:

我有一个 BaseViewController,它是 UITabBarController 的子类,并在这个视图控制器中设置我的视图。

-(void)setUpViews
{
FirstController *mainViewController = [[FirstController alloc] initAssignment:delegate.currentAssignment withMutableArray:myArray];
UINavigationController *firstNavController = [[UINavigationController alloc] initWithRootViewController:mainViewController];


SecondController *secondViewController = [[SecondController alloc] initWithNibName:@"SecondController" bundle:nil];
UINavigationController *secondNavController = [[UINavigationController alloc] initWithRootViewController:secondViewController];


self.viewControllers = [NSArray arrayWithObjects:firstNavController, secondNavController,nil];

firstNavController.tabBarItem.image = [UIImage imageNamed:@"blablabla.png"];
firstNavController.tabBarItem.title = @"Stream";


secondViewController.tabBarItem.image = [UIImage imageNamed:@"blabla.png"];
secondViewController.tabBarItem.title = @"Favourite";
}

现在我有另一个视图控制器,我称之为ViewHandlerControllerBaseViewController 的子类。在我的viewDidLoad 在这个viewHandler 中,我调用在BaseViewController 中声明的setUpViews。在我的应用程序的第一个屏幕中,当按下登录按钮时,我实例化了我的ViewHandlerController,并通过使用成功地向我的 tabcontroller 展示了导航控制器。

[[[UIApplication sharedApplication].windows objectAtIndex:0] addSubview:viewControllerHandler.view];

在我的应用程序中。有一个注销按钮。我正在使用NSNotificationCenter 来调用我的 logoutMethod,它在我的第一个屏幕中声明。我的问题是,在这个logoutMethod中,由于用户可以再次登录(logIn - logOut -logIn),如何释放先前分配的对象以避免内存压力?由于我使用的是 ARC,将我的 ViewController 设置为 NIL 是否会进行所有清理工作?

编辑:从 superview 中删除我的 ViewControllerHandler 并将其设置为 nil 是否也有助于释放其子视图?

干杯

【问题讨论】:

    标签: objective-c ios memory-management automatic-ref-counting


    【解决方案1】:

    好吧,回答你的问题(不是 ARC)——不,基本上视图控制器在释放时不会释放他的属性。但是您应该在 viewDidUnload 和(或) dealloc 方法中将您的属性设为 nil。

    如果您使用 ARC,您应该注意到某些操作可以保留您的控制器,并且在某些情况下永远无法删除。注意将对象作为委托的方法,它们可能不使用弱引用

    【讨论】:

      【解决方案2】:

      看看这个 Apple article about memory management

      您可以在 alloc 方法中使用 autorelease 或在 dealloc 中使用 for (UIView *view in [self.view subviews]) {view release};

      其实release是相对于retain的操作。当您保留时,您在分配的内存中增加了 1 个对象实例。当您致电alloc, copy, new, mutableCopy 时会发生这种情况。如果您使用 ARC,则无法释放对象,内存管理已经不是您的问题了。

      【讨论】:

      • 感谢您的回复。就我而言,我使用 arc,我想减少/避免我的应用程序中的内存压力。我想要的是紧急释放对象,因为我的应用程序正在消耗大量内存。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-13
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 2011-09-24
      • 2011-07-26
      相关资源
      最近更新 更多