我仍然在学习目标C和iPhone开发的诀窍。我的缺点是内存管理-任何帮助都将是非常感谢的。
在下面的代码中,我可以在哪里发布NSMutableArray listOfViewControllers?请记住,可以在应用程序中多次调用createTab函数,并根据用户输入动态地重新创建选项卡。此函数位于ViewController中。
如果我在退出函数之前做了
NSMutableArray *listOfViewControllers = [NSMutableArray arrayWithCapacity:1]
而不是:
NSMutableArray *listOfViewControllers = [[NSMutableArray alloc] init]当再次调用createTabs时,它仍然崩溃。
-(void) createTabs
{
//TODO - memory management - where do you release this?
NSMutableArray *listOfViewControllers = [[NSMutableArray alloc] init];
if ([briefingsArray count] > 0)
{
//add briefing(s) tab(s)
for (Briefing *briefing in briefingsArray)
{
WebViewController *briefingViewController = [[WebViewController alloc] initWithBriefing: briefing];
[listOfViewControllers addObject:briefingViewController];
[briefingViewController release];
}
[listOfViewControllers addObject:alertViewController];
//add archives tab
NSString *archiveURL = [NSString stringWithFormat: ARCHIVEURL, DeviceID()];
UIViewController *archiveViewController = [[WebViewController alloc] initWithURL:ARCHIVEURL andTitle:@"Archives" andImage:@"archive_icon.png"];
[listOfViewControllers addObject:archiveViewController];
[archiveViewController release];
}
NSArray *oldlistOfViewControllers = [self.tabBarController viewControllers];
UIViewController *vcOld = [oldlistOfViewControllers objectAtIndex:[oldlistOfViewControllers count] -1];
[listOfViewControllers addObject:vcOld];
[self.tabBarController setViewControllers:listOfViewControllers
animated:YES];
}发布于 2010-11-01 20:56:18
我最好的猜测是,它与选项卡条控制器无关。当您没有释放数组时,数组中的控制器将永远不会被释放,并且根本没有问题。因此,问题很可能来自于您的WebViewController的取消分配。
发布于 2010-11-01 21:03:28
看起来,您在这里创建的东西-- listOfViewControllers --应该是您在这里创建的任何对象的实例变量。然后,您应该在对象的-init方法中分配/init,并在dealloc中释放它。
很好的做法(通常也是必要的)是为函数调用结束后(或开始之前)希望存在的任何内容设置一个实例变量。
发布于 2010-11-01 23:20:16
在self.tabBarController setViewController: listOfViewControllers动画之后:是;之后,您可以发布您的listOfViewControllers。因为tabBarController将通过复制策略保留此listOfViewControllers。
您可以看到关于viewControllers属性的UITabBarController引用。此属性采用复制策略。
@property(nonatomic, copy) NSArray *viewControllershttps://stackoverflow.com/questions/4072660
复制相似问题