【问题标题】:UIViewController class memory issue - presentModalViewControllerUIViewController 类内存问题 - presentModalViewController
【发布时间】:2011-07-23 20:58:05
【问题描述】:

我的 iPhone 应用程序出现内存问题,我不知道发生了什么。

所以,我观察到应用程序的内存使用量在从一个 UIViewController 转到另一个时不断上升。我使用了“Allocations”工具中的“Mark Heap”工具,它表明唯一没有被释放的对象是我的 UIViewControllers。

更具体地说,我让我的两个 UIViewControllers。第一个名为 PuzzleViewController,第二个名为 Options。当应用程序启动时,会出现 PuzzleViewController。我在此处标记一个堆以设置基线,然后按“选项”按钮,该按钮将显示选项 UIViewController。我回到第一个并再次标记一个堆。在一遍又一遍地重复这些步骤之后(比如 20 次左右 :D),我观察到在每次 Heapshot 之后我有大约 22 个对象还活着。其中两个对象是我的 UIViewControllers 的实例。

我真的不知道发生了什么。

这是我切换到选项 UIViewController 的方式:

- (IBAction) onOptionsButton: (id) sender
{
    Options *viewController = [[Options alloc] init];
    [self presentModalViewController:viewController animated:YES];
    [viewController release];
}

这是我返回 PuzzleViewController 的方法:

- (IBAction) onMainMenu:(id) sender
{   
    PuzzleViewController *modalView = [[PuzzleViewController alloc] init];
    [self presentModalViewController:modalView animated:YES];
    [modalView release];
}

我的 viewDidUnload 函数被正确调用,但从未调用任何 dealloc 函数。

谢谢你, 安德烈

【问题讨论】:

  • 你是如何关闭选项视图控制器的?
  • @Firoze Lafeer,我真的不知道......对不起,我是 iPhone 编程的新手。我应该怎么做,有什么用?非常感谢!
  • 那么,您的用户如何返回 PuzzleViewController?什么按钮和代码可以做到这一点?
  • @Firoze Lafeer,我已经发布了问题的代码。
  • 这可以正常工作。如果层次结构更深,您可能会考虑使用 UINavigationController(或多个)

标签: iphone objective-c memory-management uiviewcontroller dealloc


【解决方案1】:

你应该调用dismissModalViewController,而不是再次调用presentModalViewController,也不要重新创建你的PuzzleViewController

【讨论】:

  • 非常感谢。我还有另一种更好的方法来在 UIViewControllers 之间进行洗牌吗?因为我从“主菜单”到“游戏屏幕”有 12 个 UIViewController 之类的东西。
【解决方案2】:
猜你喜欢
  • 2011-04-13
  • 1970-01-01
  • 2010-10-11
  • 2011-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多