【发布时间】:2011-10-28 20:19:55
【问题描述】:
我正在 iPhone 中创建一个测验应用程序。
这样我就可以同时使用很多标签和按钮。
如果我在一个问题后立即释放它们,则应用程序将停止。
如果我不这样做并在 dealloc 函数中释放它们,那么它会显示内存警告和应用程序崩溃。
我正在使用此代码
received memory warning. level 1
如果我设置了大约 20 个问题,那么应用程序不会崩溃,但如果我添加更多问题,它就会崩溃..
我认为这是分配内存而不是在一个问题后立即释放。
请帮帮我:(
已编辑:
选择答案后我正在使用此代码
-(void)checkAnswer:(int)theAnswerValue
{
[AVback stop];
[scrollview scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
[Challengetext setHidden:YES];
[theScore setHidden:YES];
[theQuestion setHidden:NO];
[Question setHidden:YES];
if(rightAnswer == theAnswerValue)
{
NSString *AVpath = [[NSBundle mainBundle] pathForResource:@"Winner_1" ofType:@"m4a"];
AVAudioPlayer *AVbtn1 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:AVpath] error:NULL];
[img1 setHidden:NO];
[img2 setHidden:YES];
[theQuestion setFont:[UIFont fontWithName:@"Futura" size:30]];
theQuestion.textColor = [UIColor purpleColor];
theQuestion.text = @"\n\nCorrect Answer!!";
[rightans setHidden:NO];
[Description setHidden:NO];
[ContainerView setHidden:NO];
myScore = myScore + 50;
}
else
{
NSString *AVpath = [[NSBundle mainBundle] pathForResource:@"incorrect2" ofType:@"m4a"];
AVAudioPlayer *AVbtn1 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:AVpath] error:NULL];
QuizAppUniversalAppDelegate *appDelegate = (QuizAppUniversalAppDelegate *) [[UIApplication sharedApplication] delegate];
if(appDelegate.soundIndicator == 0){
[AVbtn1 play];
}
else{
[AVback stop];
[AVstart stop];
[AVbtn1 stop];
}
[img2 setHidden:NO];
[img1 setHidden:YES];
[theQuestion setFont:[UIFont fontWithName:@"Futura" size:30]];
theQuestion.textColor = [UIColor redColor];
theQuestion.text = @"\nIncorrect Answer!!\nRight answer is : \n ";
[rightans setHidden:NO];
[Description setHidden:NO];
[ContainerView setHidden:NO];
myScore = myScore ;
}
谢谢..任何帮助将不胜感激..!!
【问题讨论】:
-
您能展示一下您是如何创建标签和按钮的吗?当一个问题得到回答时,你会怎么做?
-
对于按钮和标签,我的代码在上面的链接上,在回答问题后,我的意思是回答 CORRECt 或 INCORRECT 正在显示。让我为此编辑代码..让你有一些想法..
-
谢谢。当答案正确并且您转到下一个问题时,您是如何做到的?你在导航控制器上推送一个新的视图控制器?替换当前视图?
-
内存警告不会导致应用崩溃。崩溃可能是因为某些东西被释放了两次,例如你释放了一些你没有保留的东西,然后在 dealloc 系统上尝试释放它。
-
当时我将 NEXT 按钮设置为 [nextbtn setHidden:NO];当我点击此按钮时,它会转到 [self askQuestion];并且代码在上面的链接上..谢谢..
标签: iphone memory-management memory-leaks xcode4