如何释放 UITabBarController 及其所有视图控制器?
我以编程方式创建一个带有两个视图控制器的选项卡栏,如以下代码所示。当我不再需要标签栏时,我发现很难清理内存。苹果的文档关于发布标签栏控制器的内容非常有限。我不知道如何释放“viewControllers”数组中的所有视图控制器。我尝试打印出保留计数并发现 x & y 的保留计数高达 5。
@interface X:UIViewController
@interface Y:UIViewController
@interface Z: UIViewController {
UITabBarController *tabBar;
}
@end
@implementation Z
-(IBAction)openTabBarUp{
UITabBarController *tabBar = [[UITabBarController alloc] init];
X *x = [[X alloc] init];
Y *y = [[Y alloc] init];
tabBar.viewControllers = [NSArray arrayWithObjects: x, y, nil];
[self.view addSubView: tabBar.view];
}
这就是我尝试释放内存的方法:
-(IBAction)removeTabBar{
[tabBar.view removeFromSuperView];
[tabBar release];
tabBar = nil;
}
谢谢
Leo
I programmatically create a tab bar with two view controllers like the following code. I find it hard to clean the memory when I do not need the tab bar any more. Apple's documentation is very limited about releasing tab bar controller. I don't know how to release all the view controllers in the 'viewControllers' array. I tried to print out the retain count and found x & y's retainCount is as high as 5.
@interface X:UIViewController
@interface Y:UIViewController
@interface Z: UIViewController {
UITabBarController *tabBar;
}
@end
@implementation Z
-(IBAction)openTabBarUp{
UITabBarController *tabBar = [[UITabBarController alloc] init];
X *x = [[X alloc] init];
Y *y = [[Y alloc] init];
tabBar.viewControllers = [NSArray arrayWithObjects: x, y, nil];
[self.view addSubView: tabBar.view];
}
this is how I try to release the memory:
-(IBAction)removeTabBar{
[tabBar.view removeFromSuperView];
[tabBar release];
tabBar = nil;
}
Thanks
Leo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确实需要在
openTabBarUp
方法中使用UITabBarController *tabBar = [[UITabBarController alloc] init];
,因为您已经在头文件中声明了它的实例。您可以使用[tabBar release];
释放 tabBar,但 Apple 坚持将 tabBarController 添加为主窗口的根视图,而不是任何视图控制器的一部分。更新
Apple
UITabBarController
状态的参考文档You done need
UITabBarController *tabBar = [[UITabBarController alloc] init];
in theopenTabBarUp
method as you already have an instance of it declared in the header file. You can release the tabBar using[tabBar release];
but Apple insists to add the tabBarController as the rootview of your main window and not as part of any view controller.UPDATE
The Apple reference documents on
UITabBarController
states