删除 UITabBarController
有人发布了与此类似的问题,但没有解决方案,但也没有示例代码。所以,我想我应该在这里详细发布我的问题。
我有一款具有多种游戏模式的游戏,每种模式都有多种选择。在尝试了各种设计之后,将它们放入具有三个选项卡的 UITabBarController 中似乎是最干净的,每个选项卡对应一类游戏。我创建了一个新的 UIVIewController,它从主菜单屏幕(替换主屏幕)加载并初始化 UITabBarController,如下所示:
barController = [[UITabBarController alloc] init];
Games1 *vc1 = [[[Games1 alloc] initWithNibName:@"Games1" bundle:nil] autorelease];
Games2 *vc2 = [[[Games2 alloc] initWithNibName:@"Games2" bundle:nil] autorelease];
Games3 *vc3 = [[[Games3 alloc] initWithNibName:@"Games3" bundle:nil] autorelease];
NSArray* controllers = [NSArray arrayWithObjects:vc3, vc1, vc2, nil];
barController.viewControllers = controllers;
[self.view addSubview:barController.view];
当用户选择游戏时,我从窗口中删除 UIViewController 并按如下方式取消分配:
- (void)dealloc {
printf("Games released: barController: %d\n", [barController retainCount]);
[barController.view removeFromSuperview];
barController.viewControllers = 0;
[barController release];
barController = 0;
[super dealloc];
}
我遇到的问题是当我旋转设备时,我会崩溃。如果我直接从主屏幕启动游戏模式并旋转,则不会崩溃。我已经验证所有内容都已被释放,并且条形控制器上的保留计数为 1。有关如何消除此崩溃的任何建议吗?谢谢!
[编辑]更多信息:
barController 定义为
IBOutlet UITabBarController *barController;
:
@property (nonatomic, retain) IBOutlet UITabBarController *barController;
Someone has posted a similar question to this with no resolution, but also no sample code. So, I thought I would post my problem in detail here.
I have a game with several modes of play, each of these having several options. After playing around with various designs, it seems cleanest to put them into a UITabBarController with three tabs, one for each class of games. I created a new UIVIewController which is loaded from the main menu screen (replacing the main screen) and initializes the UITabBarController as follows:
barController = [[UITabBarController alloc] init];
Games1 *vc1 = [[[Games1 alloc] initWithNibName:@"Games1" bundle:nil] autorelease];
Games2 *vc2 = [[[Games2 alloc] initWithNibName:@"Games2" bundle:nil] autorelease];
Games3 *vc3 = [[[Games3 alloc] initWithNibName:@"Games3" bundle:nil] autorelease];
NSArray* controllers = [NSArray arrayWithObjects:vc3, vc1, vc2, nil];
barController.viewControllers = controllers;
[self.view addSubview:barController.view];
When the user selects a game, I remove the UIViewController from the window and deallocate as follows:
- (void)dealloc {
printf("Games released: barController: %d\n", [barController retainCount]);
[barController.view removeFromSuperview];
barController.viewControllers = 0;
[barController release];
barController = 0;
[super dealloc];
}
The problem I have is that when I rotate the device, I get a crash. If I launch a game mode directly from the main screen and rotate, no crash. I've verified that everything is getting deallocated, and my retain count on the bar controller is 1. Any suggestions on how to eliminate this crash? Thanks!
[Edit] A bit more info:
The barController is defined as:
IBOutlet UITabBarController *barController;
with:
@property (nonatomic, retain) IBOutlet UITabBarController *barController;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最终问题只是与 UITabBarController 外围相关。我直接在我的应用程序窗口中添加和删除 UIViewControllers,这已被证明在其他地方引起问题。添加主 UIViewController / UIView 并仅添加和删除即可解决所有问题,尽管自动释放而不是发布可能也有效。请参阅此处的讨论:
视图控制器正在即使已被释放,也发送了一条消息
UITabBarController 只是导致问题发生得更快、更明显。
It ends up the problem was only peripherally related to the UITabBarController. I was adding and removing UIViewControllers directly to my app window, which has been shown to be causing problems elsewhere. Adding a master UIViewController / UIView and only adding and removing from that fixes everything, although an autorelease instead of a release may have worked as well. See the discussion here:
View Controller being sent a message even though it has been deallocated
The UITabBarController was just causing the problem to happen much more quickly and obviously.
不要这样做:
在 -dealloc 中,您应该只从其超级视图中删除 UITabBarController 的视图并释放它。
Don't do this:
In -dealloc you should only remove the UITabBarController's view from its superview and release it.