gpt4 book ai didi

iphone - 正确释放 View 时应用程序崩溃

转载 作者:行者123 更新时间:2023-12-03 21:19:17 24 4
gpt4 key购买 nike

希望你能帮我解决这个问题。我在使用以下代码时遇到问题:

-(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 并随后将其释放。问题是,当我将 View 更改为 ThirdViewController 然后想返回到之前的 View 时,应用程序崩溃了。这就是我回到之前观点的方式:

-(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];
}

释放 View 时又出现同样的问题。如果我不释放 View ,应用程序不会崩溃,但存在大量内存泄漏,并且考虑到我有超过 15 个 ViewController,如果我长时间使用它,应用程序最终会崩溃。

有什么想法我做错了什么吗?ps:我正在使用委托(delegate)来实现 View 的动画/转换。

谢谢!

编辑: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];

}

最佳答案

不想听起来像显而易见的先生,但除非在释放其 Controller 之前切换到的 View 保留在您的委托(delegate)中,否则您将在切换回来之前对已释放的 View 进行操作,因此会崩溃。

如果您的 switchView:toView 方法在 UIViewController 上运行,而不仅仅是在 UIView 上运行,可能会更好。然后你可以保留你需要的 View 的viewController,当不再需要它时释放它。

但目前我们只能从您当前的问题描述中获取这些信息。显示 switchView:toView:switchView2:toView: 方法的代码以及崩溃日志,我们将从那里开始。

关于iphone - 正确释放 View 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6469144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com