2011-08-17 68 views
0

我是iPhone开发新手。我正在创建一个应用程序,我需要以编程方式创建按钮和标签。
所以我在.h文件中收到内存警告。 1级

@interface FirstQuizViewControlleriPad : UIViewController{ 

IBOutlet UIButton *startgame; 
UILabel *theQuestion; 
IBOutlet UIScrollView *scrollview; 
UILabel *Question; 
UILabel *Description; 
UIView *ContainerView; 
UILabel *Challengetext; 
UIImageView *img1; 
UIImageView *img2; 
UIImageView *background; 
IBOutlet UIButton *reviewbtn; 
UILabel *ans1; 
UILabel *ans2; 
UILabel *ans3; 
UILabel *ans4; 

UIButton *button; 
UIButton *button2; 
UIButton *button3; 
UIButton *button4; 
UILabel *rightans; 
UILabel *theScore; 
UIButton *nextbtn; 
UIButton *backbtn; 
NSString *setchallange; 

AVAudioPlayer *AVback; 
AVAudioPlayer *AVstart; 
NSArray *theQuiz; 
NSInteger questionNumber; 
NSInteger rightAnswer; 
NSInteger myScore; 
NSInteger finalScoreipad1; 
BOOL restartGame; 
id QuestionReview; 

} 

@property (retain , nonatomic) IBOutlet UIButton *startgame; 
@property (retain , nonatomic) NSArray *theQuiz; 
@property (retain , nonatomic) IBOutlet UILabel *theScore; 
@property (retain , nonatomic) IBOutlet UIButton *nextbtn; 
@property (retain , nonatomic) IBOutlet UIScrollView *scrollview; 
@property (retain , nonatomic) IBOutlet UIButton *backbtn; 
@property (retain , nonatomic) IBOutlet UILabel *Challengetext; 
@property (retain , nonatomic) IBOutlet UIImageView *img1; 
@property (retain , nonatomic) IBOutlet UIImageView *img2; 
@property (retain , nonatomic) id QuestionReview; 
@property (retain , nonatomic) IBOutlet UIButton *reviewbtn; 
@property (retain ,nonatomic) IBOutlet UIButton *button; 
@property (retain ,nonatomic) IBOutlet UIButton *button2; 
@property (retain ,nonatomic) IBOutlet UIButton *button3; 
@property (retain ,nonatomic) IBOutlet UIButton *button4; 
@property (retain ,nonatomic) IBOutlet UILabel *ans1; 
@property (retain ,nonatomic) IBOutlet UILabel *ans2; 
@property (retain ,nonatomic) IBOutlet UILabel *ans3; 
@property (retain ,nonatomic) IBOutlet UILabel *ans4; 
@property (retain , nonatomic) IBOutlet UILabel *Question; 
@property (retain , nonatomic) NSString *setchallange; 

-(void)askQuestion; 

在.m文件

questionNumber = questionNumber + 1; 
NSInteger row = 0; 
if(questionNumber == 1) 
{ 
    row = questionNumber - 1; 
} 
else 
{ 
    row = ((questionNumber - 1) * 10); 
} 

NSString *selected = [theQuiz objectAtIndex:row]; 
NSString *activeQuestion = [[NSString alloc] initWithFormat:@"%@", selected]; 
QuestionReview = activeQuestion; 

//lbl ref to btn1 
ans1 = [[[UILabel alloc] initWithFrame:CGRectMake(36, 292, 697, 100)] autorelease]; 
ans1.numberOfLines = 0; 
ans1.font = [UIFont systemFontOfSize:26]; 
ans1.text = [theQuiz objectAtIndex:row+1]; 
ans1.lineBreakMode = UILineBreakModeWordWrap; 
[ans1 sizeToFit]; 
[self.view addSubview:ans1]; 
//lbl ref to btn2 
ans2 = [[[UILabel alloc] initWithFrame:CGRectMake(36, 312 + ans1.frame.size.height , 697, 100)] autorelease]; 
ans2.numberOfLines = 0; 
ans2.font = [UIFont systemFontOfSize:26]; 
ans2.text = [theQuiz objectAtIndex:row+2]; 
ans2.lineBreakMode = UILineBreakModeWordWrap; 
[ans2 sizeToFit]; 
[self.view addSubview:ans2]; 
//lbl ref to btn3 
ans3 = [[[UILabel alloc] initWithFrame:CGRectMake(36 ,332 + ans1.frame.size.height + ans2.frame.size.height , 697 , 100)] autorelease]; 
ans3.numberOfLines = 0; 
ans3.font = [UIFont systemFontOfSize:26]; 
ans3.text = [theQuiz objectAtIndex:row+3] ; 
ans3.lineBreakMode = UILineBreakModeWordWrap; 
[ans3 sizeToFit]; 
[self.view addSubview:ans3]; 
//lbl ref to btn4 
ans4 = [[[UILabel alloc] initWithFrame:CGRectMake(36, 352 + ans1.frame.size.height + ans2.frame.size.height + ans3.frame.size.height, 697, 100)] autorelease]; 
ans4.numberOfLines = 0; 
ans4.font = [UIFont systemFontOfSize:26]; 
ans4.text = [theQuiz objectAtIndex:row+4]; 
ans4.lineBreakMode = UILineBreakModeWordWrap; 
[ans4 sizeToFit]; 
[self.view addSubview:ans4]; 

rightAnswer = [[theQuiz objectAtIndex:row+5] intValue]; 

Question = [[[UILabel alloc] initWithFrame:CGRectMake(22, 130, 725, 160)] autorelease]; 
Question.numberOfLines = 0; 
//Question.font = [UIFont systemFontOfSize:27]; 
Question.text = activeQuestion; 
Question.lineBreakMode = UILineBreakModeWordWrap; 
[Question setFont:[UIFont fontWithName:@"Futura" size:26]]; 
Question.backgroundColor = [UIColor clearColor]; 
[Question sizeToFit]; 
[self.view addSubview:Question]; 

//For 1st Option. 
button = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button addTarget:self 
      action:@selector(buttonOne) 
forControlEvents:UIControlEventTouchDown]; 
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap; 
button.titleLabel.font = [UIFont systemFontOfSize:24]; 
[button setTitle:[theQuiz objectAtIndex:row+1] forState:UIControlStateNormal]; 
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
button.frame = CGRectMake(34, 200 + Question.frame.size.height , 700, ans1.frame.size.height + 20); 
[self.view addSubview:button]; 
button.layer.borderWidth = 3; 
button.layer.borderColor = [[UIColor purpleColor ] CGColor]; 
button.layer.cornerRadius = 10.0; 
[button setBackgroundImage:[UIImage imageNamed:@"buttun.png"] forState:UIControlStateNormal]; 
button.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5); 

//For 2nd Option. 
button2 = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button2 addTarget:self 
      action:@selector(buttonTwo) 
    forControlEvents:UIControlEventTouchDown]; 
[button2 setTitle:[theQuiz objectAtIndex:row+2] forState:UIControlStateNormal]; 
[button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
button2.titleLabel.lineBreakMode = UILineBreakModeWordWrap; 
button2.titleLabel.font = [UIFont systemFontOfSize:24]; 
button2.frame = CGRectMake(34, 230 + Question.frame.size.height + button.frame.size.height , 700, ans2.frame.size.height + 20); 
button2.layer.borderWidth = 3; 
button2.layer.borderColor = [[UIColor darkGrayColor ] CGColor]; 
button2.layer.cornerRadius = 10.0; 
[self.view addSubview:button2]; 
[button2 setBackgroundImage:[UIImage imageNamed:@"buttun2.png"] forState:UIControlStateNormal]; 
button2.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5); 

//For 3rd Option. 
button3 = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button3 addTarget:self 
      action:@selector(buttonThree) 
    forControlEvents:UIControlEventTouchDown]; 
[button3 setTitle:[theQuiz objectAtIndex:row+3] forState:UIControlStateNormal]; 
[button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
button3.titleLabel.lineBreakMode = UILineBreakModeWordWrap; 
button3.titleLabel.font = [UIFont systemFontOfSize:24]; 
button3.frame = CGRectMake(34, 260 + Question.frame.size.height + button.frame.size.height + button2.frame.size.height , 700, ans3.frame.size.height + 20); 
button3.layer.borderWidth = 3; 
button3.layer.borderColor = [[UIColor purpleColor ] CGColor]; 
button3.layer.cornerRadius = 10.0; 

[self.view addSubview:button3]; 
[button3 setBackgroundImage:[UIImage imageNamed:@"buttun.png"] forState:UIControlStateNormal]; 
button3.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5); 

//For 4th Option. 
button4 = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button4 addTarget:self 
      action:@selector(buttonFour) 
    forControlEvents:UIControlEventTouchDown]; 
[button4 setTitle:[theQuiz objectAtIndex:row+4] forState:UIControlStateNormal]; 
[button4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
button4.titleLabel.lineBreakMode = UILineBreakModeWordWrap; 
button4.titleLabel.font = [UIFont systemFontOfSize:24]; 
button4.frame = CGRectMake(34, 290 + Question.frame.size.height + button.frame.size.height + button2.frame.size.height + button3.frame.size.height , 700, ans4.frame.size.height + 20); 
button4.layer.borderWidth = 3; 
button4.layer.borderColor = [[UIColor darkGrayColor ] CGColor]; 
button4.layer.cornerRadius = 10.0; 
[self.view addSubview:button4]; 
[button4 setBackgroundImage:[UIImage imageNamed:@"buttun2.png"] forState:UIControlStateNormal]; 
button4.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5); 

NSString *strright = [theQuiz objectAtIndex:row+7]; 
NSString *strtext = [[NSString alloc] initWithFormat:@"%@" , strright]; 

rightans = [[UILabel alloc] initWithFrame:CGRectMake(50, 330, 650, 150)]; 
rightans.numberOfLines = 0; 
rightans.text = strtext; 
rightans.backgroundColor = [UIColor clearColor]; 
rightans.font = [UIFont systemFontOfSize:26]; 
[self.view addSubview:rightans]; 
[rightans sizeToFit]; 
[rightans setHidden:YES]; 

//Description. 
NSString *des = [theQuiz objectAtIndex:row+6]; 
NSString *destext = [[NSString alloc] initWithFormat:@"\n> Explanation :\n%@\n\n" , des]; 

Description = [[[UILabel alloc] initWithFrame:CGRectMake(40, 400 + rightans.frame.size.height , 690, 150)] autorelease]; 
Description.numberOfLines = 0; 
// Description.font = [UIFont systemFontOfSize:13.5]; 
Description.text = destext; 
[Description setFont:[UIFont fontWithName:@"Futura" size:26]]; 
Description.lineBreakMode = UILineBreakModeWordWrap; 
Description.backgroundColor = [UIColor clearColor]; 
[Description sizeToFit]; 
[self.view addSubview:Description]; 
[Description setHidden:YES]; 


ContainerView = [[UIView alloc] initWithFrame:CGRectMake(30, 400 + rightans.frame.size.height , 700 ,Description.frame.size.height)]; 
ContainerView.backgroundColor = [UIColor clearColor]; 
ContainerView.layer.cornerRadius = 10.0; 
ContainerView.layer.borderColor = [[UIColor grayColor] CGColor]; 
ContainerView.layer.borderWidth = 3; 
// [ContainerView addSubview:Description]; 
[self.view addSubview:ContainerView]; 
[ContainerView setHidden:YES]; 

//For Challenge Text 
NSString *challange = [theQuiz objectAtIndex:row+8]; 
    setchallange = [[NSString alloc] initWithFormat:@"%@" , challange];//Memory leak here 
Challengetext.text = setchallange; 



//Image For Every Challenge 
background = [[[UIImageView alloc] initWithFrame:CGRectMake(15.0f, 15.0f, 130.0f, 110.0f)] autorelease]; 
[background setImage:[UIImage imageNamed:[theQuiz objectAtIndex: row + 9]]]; 
[self.view addSubview:background]; 

这样在dealloc的功能,我释放所有的标签和按钮和我已经创建的其他对象,但仍然我得到这个警告的内存泄漏(内存收到警告等级1和一些次2级)
我搜索我的记忆中被泄露,其中,它是在的NSString setchallenge泄漏

请帮我这个..
任何帮助将不胜感激..

谢谢..

+0

嘿从堆栈溢出,这个问题应该可以帮助您... http://stackoverflow.com/questions/2915247/iphone-os-memory-warnings-what-do-the-different-levels-mean – PJR

回答

1

你创建与每个调用一个新的字符串setchallenge = [[NSString的页头] ...

应该是:

//For Challenge Text 
NSString *challange = [theQuiz objectAtIndex:row+8]; 
Challengetext.text = [NSString stringWithFormat:@"%@" , challange]; 

stringWithFormat返回一个自动释放的字符串,这将在下次自动释放池被耗尽时取消分配。

+0

谢谢!让我试试这个.. :) – iUser

+0

它现在没有泄漏.. :))) 谢谢Flyingdiver !!非常感谢!! :)) – iUser

1

既然你alloced记忆,你需要释放它。任何时候你使用alloc,copy或new,都要记得始终释放。它不是一个好主意,只在dealloc中释放所有东西。当你完成它们时释放对象。例如:

setchallange = [[NSString alloc] initWithFormat:@"%@" , challange];//Memory leak here 
Challengetext.text = setchallange; 
[setchallange release] 

也可以结帐输出iOS memory management guide。我相信这会有所帮助。

+0

感谢Manish .. !!但如果在这里我释放setchallange,那么应用程序正在停止,因为我必须多次使用该字符串来逐个显示问题。所以我释放它dealloc功能... – iUser

+0

你仍然可以释放。当您将挑战挑战分配给Challengetext.text时,它会将保留计数增加1并处理事情。你应该真的阅读内存管理指南。 –

+0

感谢Manish :) .. IT似乎有用.. – iUser

0

可能对某些对象,你分配1倍以上,一旦松开。只是检查出来,我希望它会解决。

+0

这是真的..但我怎么能释放它后,每次使用..我的意思是有任何方式来重用它? – iUser