导航控制问题

时间:2011-07-13 22:20:28

标签: objective-c navigation controls

我正在使用一些方法来加载新的.xib并返回主菜单。但是大约五次之后它会因使用太多内存而崩溃。我需要能够多次返回主菜单和游戏。我应该用于导航控件的任何其他方法。

主菜单部分:

GameViewController* game = [[GameViewController alloc initWithNibName:@"GameViewController" bundle:nil];
[self.navigationController pushViewController:game animated:NO];

游戏部分返回主菜单:

[self.navigationController popViewControllerAnimated:NO];

这是viewdidLoad { - (void)viewDidLoad {     [super viewDidLoad];     [self StartTimer];

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

}

}

1 个答案:

答案 0 :(得分:0)

在您的视图控制器上尝试autorelease

GameViewController* game = [[[GameViewController alloc initWithNibName:@"GameViewController" bundle:nil] autorelease];

导航控制器将获取传递给它的视图控制器的所有权,因此您不必保留对它的引用。但是你无法在不释放它们的情况下反复分配GameViewControllers。 autorelease对此非常有用。如果您愿意,也可以在将其传递给导航控制器后将其释放:

GameViewController* game = [[GameViewController alloc initWithNibName:@"GameViewController" bundle:nil];
[self.navigationController pushViewController:game animated:NO];
[game release];
game = nil;

编辑: 因此,如果您已经发布了game对象,则它必须是GameViewController类本身内存泄漏。

您在GameViewController类中分配,复制或保留的内容,您应该在dealloc方法中释放(如果您是alloc /,也可能在viewDidUnload方法中复制/保留viewDidLoad方法。)

如果您想了解更多细节,iOS Memory Management Programming Guide可能会有所帮助。

如果您想发布GameViewController课程中的相关代码,我相信有人可以帮助您确定内存泄漏。

您还可以尝试Instruments

中的泄漏工具

编辑2:

我假设你的IBOutlets课程中有多个GameViewController已与...相关联...

不知道您是否已经这样做了,但是在您的viewDidUnload方法和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,则该对象将为released。因此,retain关键字的任何属性都应设置为nil(或发布支持ivar)。