我是iPhone开发的新手。我正在创建一个应用程序,我需要以编程方式创建按钮和标签 所以我在.h文件中这样做
@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;
<。>文件中的
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 :(得分:1)
由于您分配了内存,因此您也需要释放内存。无论何时使用alloc,copy或new,请记住始终发布。仅在dealloc发布所有内容并不是一个好主意。完成后释放对象。例如:
setchallange = [[NSString alloc] initWithFormat:@"%@" , challange];//Memory leak here
Challengetext.text = setchallange;
[setchallange release]
同时结帐iOS memory management guide。我相信它会有所帮助。
答案 1 :(得分:1)
每次调用setchallenge时都会创建一个新字符串= [[NSString alloc] ....
应该是:
//For Challenge Text
NSString *challange = [theQuiz objectAtIndex:row+8];
Challengetext.text = [NSString stringWithFormat:@"%@" , challange];
stringWithFormat返回一个自动释放的字符串,该字符串将在下次自动释放池耗尽时被释放。
乔
答案 2 :(得分:0)
可能是某些对象分配了多次并释放一次。看看它我希望它能解决。