添加到 UINavigationController 时 UIViewController 未释放
下面是一些减少的代码来演示我遇到的问题。
我分配并初始化一个视图控制器。 init 方法是标准的 Objective C init,因此视图控制器还没有执行任何操作。然后我创建一个 navigationController,使用新创建的视图控制器初始化根。 然后我释放视图控制器,因为导航现在已经有了它,但我不需要它。 在下一行中, nav 被释放,我希望视图控制器的 dealloc/viewDidUnload 将被调用。 相反,他们不是。 如果我没有将 sportsViewController 放在导航控制器上,则在 sportsViewController 释放后会调用 dealloc 。 我做错了什么? (顺便说一句,我还有其他类似的代码可以正常工作)
SportsViewController *sportsViewController = [[SportsViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:sportsViewController];
[sportsViewController release];
[nav release];
Below is reduced bit of code to demonstrate a problem I am a having.
I allocate and init a view controller. The init method is the standard Objective C init, so the view controller isn't doing anything yet. Then I create a navigationController initializing the root with the newly created view controller.
I then release the view controller since the nav has it now and I don't need it.
In the next line nav is released and I would expect that the view controller's dealloc/viewDidUnload would be called.
Instead they are not.
If I don't put the sportsViewController on the nav controller, dealloc is called after sportsViewController is released.
What am I doing wrong?
(BTW I have other similar pieces of code that work correctly)
SportsViewController *sportsViewController = [[SportsViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:sportsViewController];
[sportsViewController release];
[nav release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论