Objective c 导航控制问题
我正在使用一些方法加载一个新的.xib并返回主菜单。然而,大约五次之后,由于使用了太多内存,它崩溃了。我需要能够多次返回主菜单和游戏。导航控件应使用的任何其他方法 主菜单部分:Objective c 导航控制问题,objective-c,navigation,controls,Objective C,Navigation,Controls,我正在使用一些方法加载一个新的.xib并返回主菜单。然而,大约五次之后,由于使用了太多内存,它崩溃了。我需要能够多次返回主菜单和游戏。导航控件应使用的任何其他方法 主菜单部分: GameViewController* game = [[GameViewController alloc initWithNibName:@"GameViewController" bundle:nil]; [self.navigationController pushViewController:game anima
GameViewController* game = [[GameViewController alloc initWithNibName:@"GameViewController" bundle:nil];
[self.navigationController pushViewController:game animated:NO];
要返回主菜单的游戏部分:
[self.navigationController popViewControllerAnimated:NO];
这是viewdidLoad
{
-空隙率{
[超级视图下载];
[自我启动者]
TotalSeconds = 0;
GameCenterTotalSeconds = 0;
timeSec = 0;
timeMin = 0;
Background = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 480, 320)] autorelease];
Background.image = [UIImage imageWithContentsOfFile:[ [ NSBundle mainBundle] pathForResource:@"Background" ofType:@"png"]];
[self.view addSubview:Background];
timeLabel.textColor = [UIColor redColor];
[self.view addSubview:timeLabel];
NumberLabel = [[[UIImageView alloc] initWithFrame:CGRectMake(0, -4, 60, 70)] autorelease];
NumberLabel.image = [UIImage imageWithContentsOfFile:[[ NSBundle mainBundle] pathForResource:@"Number" ofType:@"png"]];
[self.view addSubview:NumberLabel];
QuestionNumber = [[[UILabel alloc] initWithFrame:CGRectMake(23, 17, 20, 20)] autorelease];
QuestionNumber.text = @"1";
QuestionNumber.textColor = [UIColor redColor];
QuestionNumber.backgroundColor = [UIColor clearColor];
[QuestionNumber setFont:[UIFont fontWithName:@"Marker Felt" size:30]];
[self.view addSubview:QuestionNumber];
numberLives = 1;
appDelegate = (OppositeMoronTestAppDelegate *)[[UIApplication sharedApplication]delegate];
musicButton = [[[UIButton buttonWithType:UIButtonTypeCustom] retain] autorelease];
musicButton.frame = CGRectMake(5, 283, 35, 35);
musicButton.backgroundColor = [UIColor clearColor];
if (appDelegate.shouldPlayMusic == YES) {
UIImage *Image = [UIImage imageWithContentsOfFile:[[ NSBundle mainBundle] pathForResource:@"MusicOn" ofType:@"png"]];
[musicButton setBackgroundImage:Image forState:UIControlStateNormal];
[musicButton addTarget:self action:@selector(TurnMusicOff) forControlEvents:UIControlEventTouchUpInside];
} else {
UIImage *Image = [UIImage imageWithContentsOfFile:[[ NSBundle mainBundle] pathForResource:@"MusicOff" ofType:@"png"]];
[musicButton setBackgroundImage:Image forState:UIControlStateNormal];
[musicButton addTarget:self action:@selector(TurnMusicOn) forControlEvents:UIControlEventTouchUpInside];
}
[self.view addSubview:musicButton];
[self showQuestion1];
}
}在视图控制器上尝试自动释放:
GameViewController* game = [[[GameViewController alloc initWithNibName:@"GameViewController" bundle:nil] autorelease];
导航控制器将拥有传递给它的视图控制器的所有权,因此您不必保留对它的引用。但是,如果不释放GameViewController,您就不能一遍又一遍地分配它们。自动释放在这方面很有用。如果愿意,也可以在将其传递给导航控制器后释放:
GameViewController* game = [[GameViewController alloc initWithNibName:@"GameViewController" bundle:nil];
[self.navigationController pushViewController:game animated:NO];
[game release];
game = nil;
编辑:
因此,如果您已经释放了游戏对象,那么它一定是GameViewController类本身的内存泄漏
注释您在GameViewController类中alloc、copy或retain,如果您在viewDidLoad方法中alloc/copy/retain,则您应该在dealloc方法中发布,也可能在viewDidUnload方法中发布
如果您想了解更多详细信息,此选项可能会有所帮助
如果您想发布GameViewController类中的相关代码,我相信有人会帮助您锁定内存泄漏
您也可以在中尝试泄漏工具
编辑2:
我假设您的GameViewController类中有多个IBOutlet连接到属性
不知道您是否已经这样做了,但在ViewDiUnload方法和dealloc方法中,您必须将所有这些IBOutlets属性设置为nil才能释放它们,如下所示:
- viewDidUnload
{
//... whatever comes before
self.timeLabel = nil;
self.NumberLabel = nil;
//... etc
}
- dealloc
{
//... whatever comes before
self.timeLabel = nil;
self.NumberLabel = nil;
//... etc
[super dealloc];
}
通常,如果您有任何使用retain声明的属性,这意味着当您设置该属性时,对象将被保留。如果将该属性设置为nil,则会为您释放该属性中的对象。因此,任何带有retain关键字的属性都应该设置为nil或backing ivar release。在再次分配游戏之前,您是否要释放游戏?好的,您没有将代码的任何部分发布到发布游戏的地方。。。我认为如果你有内存泄漏问题,这将是相关的。你能在发布游戏的那部分代码之后立即发布代码吗?那么我假设这是GameViewController类中的内存泄漏。如果您可以编辑原始问题并发布该类中的一些相关代码,可能会有人提供帮助。哦,如果您将removeFromSuperview添加到导航控制器中,您不应该调用removeFromSuperview。。。导航控制器将负责执行转换和删除视图。但我不认为这会导致泄漏。当我返回主菜单时,它会释放GameViewController类使用的所有内存。抱歉,removeFromSuperview不是正确的代码。我正在测试一种不同的方式来启动视图。使用导航控制器和添加和删除视图。