[英]iphone UITabBarController memory management
In my function - (void)viewDidLoad
: 在我的功能- (void)viewDidLoad
:
FirstViewController *first = [[FirstViewController alloc]init];
SecondViewController *second = [[SecondViewController alloc]init];
ThirdViewController *third = [[ThirdViewController alloc]init];
ForthViewController *forth = [[ForthViewController alloc]init];
FifthViewController *fifth = [[FifthViewController alloc]init];
first.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"first" image:[UIImage imageNamed:@"FirstTab.png"] tag:0];
second.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"second" image:[UIImage imageNamed:@"SecondTab.png"] tag:1];
third.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"third" image:[UIImage imageNamed:@"ThirdTab.png"] tag:2];
forth.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"forth" image:[UIImage imageNamed:@"ForthTab.png"] tag:3];
fifth.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"fifth" image:[UIImage imageNamed:@"FifthTab.png"] tag:3];
UINavigationController *navigationFirst = [[UINavigationController alloc]initWithRootViewController:first];
UINavigationController *navigationSecond = [[UINavigationController alloc]initWithRootViewController:second];
UINavigationController *navigationThird = [[UINavigationController alloc]initWithRootViewController:third];
UINavigationController *navigationForth = [[UINavigationController alloc]initWithRootViewController:forth];
UINavigationController *navigationFifth = [[UINavigationController alloc]initWithRootViewController:fifth];
// [first release];
// [second release];
// [third release];
// [forth release];
// [first release];
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:navigationFirst,navigationSecond,navigationThird,navigationForth,navigationFifth, nil];
[navigationFirst release];
[navigationSecond release];
[navigationThird release];
[navigationForth release];
[navigationFifth release];
self.tabbarController = [[UITabBarController alloc]init];
self.tabbarController.view.frame = CGRectMake(0, 0, 320, 480);
self.tabbarController.viewControllers = array;
[array release];
I intend to add five UINavigationControllers to tabcontroller,if I don't comment the fellowing code,it will crash: 我打算将五个UINavigationControllers添加到tabcontroller,如果不注释该代码,它将崩溃:
// [first release];
// [second release];
// [third release];
// [forth release];
// [first release];
but I want to know what's the problem,I think it is correct to add thees code. 但是我想知道是什么问题,我认为添加您的代码是正确的。
you are releasing first object twice. 您将释放第一个对象两次。 thats why your code is crashing. 这就是为什么您的代码崩溃的原因。 replace [first release] with [fifth release] 用[第五个版本]替换[第一个版本]
You should release Navigation controllers in the end 您应该最后释放导航控制器
UINavigationController *navigationFirst = [[UINavigationController alloc]initWithRootViewController:first];
UINavigationController *navigationSecond = [[UINavigationController alloc]initWithRootViewController:second];
UINavigationController *navigationThird = [[UINavigationController alloc]initWithRootViewController:third];
UINavigationController *navigationForth = [[UINavigationController alloc]initWithRootViewController:forth];
UINavigationController *navigationFifth = [[UINavigationController alloc]initWithRootViewController:fifth];
[first release];
[second release];
[third release];
[forth release];
[first release];
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:navigationFirst,navigationSecond,navigationThird,navigationForth,navigationFifth, nil];
self.tabbarController = [[UITabBarController alloc]init];
self.tabbarController.view.frame = CGRectMake(0, 0, 320, 480);
self.tabbarController.viewControllers = array;
[navigationFirst release];
[navigationSecond release];
[navigationThird release];
[navigationForth release];
[navigationFifth release];
[array release];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.