如何释放 UITabBarController 及其所有视图控制器?

发布于 2024-12-04 19:52:50 字数 759 浏览 0 评论 0原文

我以编程方式创建一个带有两个视图控制器的选项卡栏,如以下代码所示。当我不再需要标签栏时,我发现很难清理内存。苹果的文档关于发布标签栏控制器的内容非常有限。我不知道如何释放“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌无敌 2024-12-11 19:52:50
-(IBAction)openTabBarUp{
  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];

}

您确实需要在 openTabBarUp 方法中使用 UITabBarController *tabBar = [[UITabBarController alloc] init]; ,因为您已经在头文件中声明了它的实例。您可以使用 [tabBar release]; 释放 tabBar,但 Apple 坚持将 tabBarController 添加为主窗口的根视图,而不是任何视图控制器的一部分。

更新

Apple UITabBarController 状态的参考文档

部署标签栏界面时,必须将此视图安装为
窗口的根目录。与其他视图控制器不同,标签栏
界面永远不应该安装为另一个视图的子视图
控制器。

-(IBAction)openTabBarUp{
  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];

}

You done need UITabBarController *tabBar = [[UITabBarController alloc] init]; in the openTabBarUp 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

When deploying a tab bar interface, you must install this view as the
root of your window. Unlike other view controllers, a tab bar
interface should never be installed as a child of another view
controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文