我想从 MainWindow 内的 UITabBarController 中删除所有项目。我可以这样实现:
self.tabViewController.viewControllers = [NSArray array];
self.tabViewController.customizableViewControllers = [NSArray array];
但是那里的旧 Controller 怎么样?这是更正确的方法吗?
- (void)cleanCurrentTabbar {
for (id ctrl in self.tabViewController.customizableViewControllers) {
[ctrl release];
}
for (id ctrl in self.tabViewController.viewControllers) {
[ctrl release];
}
self.tabViewController.viewControllers = [NSArray array];
self.tabViewController.customizableViewControllers = [NSArray array];
}
最佳答案
您的第二个选项在内存管理方面也是更正确的概念,因为它在使其为零之前释放所有分配的资源。
但是您可以分配 nil
,而不是给它一个空数组(它本身就是一个自动释放的对象,如 [NSArray array])。
- (void)cleanCurrentTabbar {
for (id ctrl in self.tabViewController.customizableViewControllers) {
[ctrl release];
}
for (id ctrl in self.tabViewController.viewControllers) {
[ctrl release];
}
self.tabViewController.viewControllers = nil;
self.tabViewController.customizableViewControllers = nil;
}
正如 James Webster 在评论中所说:
“您可能需要也可能不需要释放,具体取决于 viewController 和可定制 ViewController 的属性类型”
希望对你有帮助。
关于objective-c - 从 UITabBarController 中删除所有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7253658/