2011-08-17 103 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