添加没有navigationController的子视图时正确释放ViewController
我经常遇到的问题是,将 ViewController.view 添加为不在导航控制器上的子视图时,无法正确创建和销毁 ViewController。
例如:
MyViewController *myViewController = [[MyViewController alloc] init];
[currentView addSubView:myViewController.view];
[myViewController release];
如果它是无控制器视图并且没有用户必须与之交互的 UIControl,那么效果很好。但是向该视图的视图控制器发送消息会导致EXEC_BAD_ACCESS,因为它们不再位于内存中。
MyViewController *myViewController = [[MyViewController alloc] init];
[currentView addSubView:myViewController.view];
这在发送消息时有效,但它是内存泄漏并被静态分析器捕获。
将其设置为当前视图控制器的属性有时会起作用。但是,如果我需要创建一堆具有未知数量的 MyViewController 并将它们添加到类似 UIScrollView 的内容中,那也不起作用。
for (int i = 0; i < [myViewControllers count]; i++) {
MyViewController *myTmpViewController = [[MyViewController alloc] init];
[myCurrentUIScrollView addSubview:myTmpViewController.view];
[myTmpViewController release];
}
如果 myTmpViewController 有用户交互或类似的东西,仍然会崩溃。如何添加它并正确释放它?
Something I run into a lot is not being able to create and destroy a ViewController properly when adding the ViewController.view as a subview not on a navigation controller.
for example:
MyViewController *myViewController = [[MyViewController alloc] init];
[currentView addSubView:myViewController.view];
[myViewController release];
That Works great if it is a controllerless view and there are no UIControls that the user must interact with. But sending messages to the view controller of that view causes EXEC_BAD_ACCESS because they are no longer in memory.
MyViewController *myViewController = [[MyViewController alloc] init];
[currentView addSubView:myViewController.view];
This works when sending messages, however it is a memory leak and is caught by the static analyzer.
Setting it as a property of the current view controller works sometimes. But if I need to create a bunch with an unknown number of MyViewControllers and add them to something like a UIScrollView, that doesn't work either.
for (int i = 0; i < [myViewControllers count]; i++) {
MyViewController *myTmpViewController = [[MyViewController alloc] init];
[myCurrentUIScrollView addSubview:myTmpViewController.view];
[myTmpViewController release];
}
Still gonna crash if myTmpViewController has user interaction or something similar. How does one go about adding this, and releasing it properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以拥有一个 NSMutableArray 并在其中添加控制器。
You can have a NSMutableArray and add the controllers there.
您可以将指向视图控制器的指针存储在 ivar 中,然后在
dealloc
方法中释放它。You could store a pointer to the view controller in an ivar and then release it in your
dealloc
method.如果这样的子视图具有有限的“控制需求”,那么您可能会考虑从 UIView 进行子类化并拥有视图控件本身(例如成为其自己的委托),
否则您需要决定这些视图控制器的最合乎逻辑的“所有者”(通常是父视图的视图控制器)并使它们成为其所有者的 ivars 。
If such a subview has limited 'controlling needs', then you might consider to subclass from UIView and have the view control itself (e.g. Be its own delegate)
Otherwise you need to decide for the most logical 'owner' of these viewcontrollers (often the viewcontroller of the parentview) and make them ivars of their owner.