希望你能帮我解决这个问题。我遇到以下代码问题:
-(IBAction)swapViews:(id)sender{
myappAppDelegate *delegate = (myappAppDelegate *) [[UIApplication sharedApplication] delegate];
ThirdViewController *thirdView = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
[delegate switchView:self.view toView:thirdView.view];
[thirdView release];
}
正如你所看到的,我分配了我的 ViewController 并在之后释放了它。问题是当我将视图更改为我的 ThirdViewController 然后想要返回到前一个视图时,应用程序崩溃了。这就是我如何回到我以前的观点:
-(IBAction)goBack:(id)sender{
myappAppDelegate *delegate = (myappAppDelegate *) [[UIApplication sharedApplication] delegate];
FirstViewController *firstView = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
[delegate switchView2:self.view toView:firstView.view];
[firstView release];
}
释放视图时再次出现同样的问题。如果我不释放视图,应用程序不会崩溃,但是会有很多内存泄漏,并且考虑到我有超过 15 个 ViewController,如果我长时间使用它,应用程序最终会崩溃。
任何想法我做错了什么?ps:我正在为视图的动画/过渡使用委托。
谢谢!
编辑:switchView:toView:下面的代码
-(void)switchView:(UIView *)view1 toView:(UIView *)view2 {
[UIView beginAnimations:@"Animation" context:nil];
[UIView setAnimationDuration:0.75];
[UIViewsetAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.window cache:YES];
[view1 removeFromSuperview];
[window addSubview:view2];
[UIView commitAnimations];
}