【问题标题】:Why is UIViewController deallocated although it is on screen为什么 UIViewController 尽管它在屏幕上却被释放
【发布时间】:2012-09-16 07:40:36
【问题描述】:

不知道发生了什么。

我正在构建一个使用带有分页的滚动视图的 iPhone 应用程序。滚动视图包含几个视图,它们的视图控制器通过调用从情节提要中加载:

[self.storyboard instantiateViewControllerWithIdentifier:@"identifier"];

我将视图控制器添加到一个可变数组中,并将它们的视图作为子视图添加到滚动视图中。 出现在屏幕上后,视图控制器被释放,因此目标操作不再起作用。

当我启用 Zombie Objects 时,调试器在向控制器发送操作时写了这个:

*** -[StreamingViewController performSelector:withObject:withObject:]:消息发送到已释放实例 0x914f0e0

我确实尝试过解决此问题,但没有成功,因此非常感谢您的每一个帮助。

更多代码:

StreamingViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"Streaming View Controller"];

[self.pageControllers addObject:controller]; // adding view controller to mutable array

controller.view.frame = self.scrollView.frame;
[self.scrollView addSubview:controller.view];

【问题讨论】:

  • pageControllers 是如何声明的?
  • pageControllers 被声明为私有@property

标签: iphone objective-c ios uiviewcontroller uiscrollview


【解决方案1】:

如果您使用 ARC,则当您不再拥有指向某事物的有效指针时,通常会退出。因此,在您的情况下,您的视图控制器超出范围并被释放。该视图仍然保留,因为它由self.scrollView 持有。将视图控制器添加到数组可以解决这个问题,但前提是数组已正确实例化。属性不会自动实例化。除非您覆盖访问器,否则使用该属性也不会为您实例化它。由于 Cocoa 的性质,您不会通过尝试将对象添加到 nil 属性而收到任何错误或消息,因为它是完全有效的代码。

检查您是如何创建 pageControllers 并确保您正确地实例化它 之前使用它。

例如self.pageControllers = [[NSMutableArray alloc] init];

【讨论】:

  • 3 年后仍然有用!谢谢!
猜你喜欢
  • 2015-11-07
  • 1970-01-01
  • 2021-03-26
  • 1970-01-01
  • 2011-05-20
  • 1970-01-01
  • 1970-01-01
  • 2021-12-06
  • 1970-01-01
相关资源
最近更新 更多