我听说在执行代码期间多次调用viewDidLoad()
方法。如果i alloc
在此方法中有一些视图,它将创建一个新的pointer
每次调用它而不释放它们。
这是我的方法
-(void)viewDidLoad()
[b1 release];
[b2 release];
[b3 release];
[b4 release];
[b5 release];
[b6 release];
[b7 release];
[s1 release];
[s2 release];
[s3 release];
[s4 release];
[s5 release];
[s6 release];
[s7 release];
[bg release];
[bgview release];
[player release];
window = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, 1026, 768)];
[window setImage:[UIImage imageNamed:@"backgrd99.png"]];
[self.view addSubview:window];
tree1 = [[UIImageView alloc]initWithFrame:CGRectMake(860,100,258,543)];
[tree1 setImage:[UIImage imageNamed:@"moving trees1.png"]];
[self.view addSubview:tree1];
//[tree1 release];
/*tree2 = [[UIImageView alloc]initWithFrame:CGRectMake(860,200,327,521)];
[tree2 setImage:[UIImage imageNamed:@"moving trees00010001.png"]];
[self.view addSubview:tree2];
[tree2 release];*/
bg = [[UIImageView alloc]initWithFrame:CGRectMake(-1,-1, 1026, 768)];
[bg setImage:[UIImage imageNamed:@"room99.png"]];
[self.view addSubview:bg];
bgsofa =[[UIImageView alloc]initWithFrame:CGRectMake(-1,-1
, 1026, 768)];
[self.view addSubview:bgsofa];
bgview = [[UIView alloc]initWithFrame:CGRectMake(230, 358, 80, 68)];
//[bgview setBackgroundColor:[UIColor cyanColor]];
[self.view addSubview:bgview];
bgview1 = [[UIView alloc]initWithFrame:CGRectMake(500,100, 500,200)];
//[bg setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:bgview1];
sofapattelview =[[UIView alloc]initWithFrame:CGRectMake(530, 20, 440, 97)];
[sofapattelview setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:sofapattelview];
sofapattelimg =[[UIImageView alloc]initWithFrame:CGRectMake(0,0, 440, 97)];
[sofapattelimg setImage:[UIImage imageNamed:@"sofa-button1.png"]];
[sofapattelview addSubview:sofapattelimg];
// create the view that will execute our animation
/*colorbt =[UIButton buttonWithType:UIButtonTypeRoundedRect ];
colorbt.frame = CGRectMake(50, 50, 100, 50);
[colorbt setTitle:@"Paint" forState:UIControlStateNormal];
[colorbt addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:colorbt];*/
pattel = [[UIView alloc]initWithFrame:CGRectMake(58, 20, 440, 97)];
[pattel setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:pattel];
pattelimg =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 440, 97)];
[pattelimg setImage:[UIImage imageNamed:@"paint-button.png"]];
[pattel addSubview:pattelimg];
b1 =[[UIButton alloc] initWithFrame:CGRectMake(15, 33, 53, 51)];
[b1 addTarget:self action:@selector(B1) forControlEvents:UIControlEventTouchUpInside];
//[b1 setBackgroundColor:[UIColor cyanColor]];
[pattel addSubview:b1];
b2 =[[UIButton alloc] initWithFrame:CGRectMake(75, 33, 53, 51)];
[b2 addTarget:self action:@selector(B2) forControlEvents:UIControlEventTouchUpInside];
//[b2 setBackgroundColor:[UIColor cyanColor]];
[pattel addSubview:b2];
b3 =[[UIButton alloc] initWithFrame:CGRectMake(137, 33, 53, 51)];
[b3 addTarget:self action:@selector(B3) forControlEvents:UIControlEventTouchUpInside];
//[b3 setBackgroundColor:[UIColor cyanColor]];
[pattel addSubview:b3];
b4 =[[UIButton alloc] initWithFrame:CGRectMake(196, 33, 53, 51)];
[b4 addTarget:self action:@selector(B4) forControlEvents:UIControlEventTouchUpInside];
//[b4 setBackgroundColor:[UIColor cyanColor]];
[pattel addSubview:b4];
b5 =[[UIButton alloc] initWithFrame:CGRectMake(257, 33, 53, 51)];
[b5 addTarget:self action:@selector(B5) forControlEvents:UIControlEventTouchUpInside];
//[b5 setBackgroundColor:[UIColor cyanColor]];
[pattel addSubview:b5];
b6 =[[UIButton alloc] initWithFrame:CGRectMake(318, 33, 53, 51)];
[b6 addTarget:self action:@selector(B6) forControlEvents:UIControlEventTouchUpInside];
//[b6 setBackgroundColor:[UIColor cyanColor]];
[pattel addSubview:b6];
b7 =[[UIButton alloc] initWithFrame:CGRectMake(377, 33, 53, 51)];
[b7 addTarget:self action:@selector(B7) forControlEvents:UIControlEventTouchUpInside];
//[b7 setBackgroundColor:[UIColor cyanColor]];
[pattel addSubview:b7];
s1 =[[UIButton alloc] initWithFrame:CGRectMake(15, 33, 53, 51)];
[s1 addTarget:self action:@selector(S1) forControlEvents:UIControlEventTouchUpInside];
//[s1 setBackgroundColor:[UIColor blackColor]];
[sofapattelview addSubview:s1];
s2 =[[UIButton alloc] initWithFrame:CGRectMake(75, 33, 53, 51)];
[s2 addTarget:self action:@selector(S2) forControlEvents:UIControlEventTouchUpInside];
//[s2 setBackgroundColor:[UIColor blackColor]];
[sofapattelview addSubview:s2];
s3 =[[UIButton alloc] initWithFrame:CGRectMake(137, 33, 53, 51)];
[s3 addTarget:self action:@selector(S3) forControlEvents:UIControlEventTouchUpInside];
//[s3 setBackgroundColor:[UIColor blackColor]];
[sofapattelview addSubview:s3];
s4 =[[UIButton alloc] initWithFrame:CGRectMake(196, 33, 53, 51)];
[s4 addTarget:self action:@selector(S4) forControlEvents:UIControlEventTouchUpInside];
//[s4 setBackgroundColor:[UIColor blackColor]];
[sofapattelview addSubview:s4];
s5 =[[UIButton alloc] initWithFrame:CGRectMake(257, 33, 53, 51)];
[s5 addTarget:self action:@selector(S5) forControlEvents:UIControlEventTouchUpInside];
//[s5 setBackgroundColor:[UIColor blackColor]];
[sofapattelview addSubview:s5];
s6 =[[UIButton alloc] initWithFrame:CGRectMake(318, 33, 53, 51)];
[s6 addTarget:self action:@selector(S6) forControlEvents:UIControlEventTouchUpInside];
//[s6 setBackgroundColor:[UIColor blackColor]];
[sofapattelview addSubview:s6];
s7 =[[UIButton alloc] initWithFrame:CGRectMake(377, 33, 53, 51)];
[s7 addTarget:self action:@selector(S7) forControlEvents:UIControlEventTouchUpInside];
//[s7 setBackgroundColor:[UIColor blackColor]];
[sofapattelview addSubview:s7];
FileURL = [[NSURL alloc]initFileURLWithPath:[[NSBundle mainBundle]pathForResource:@"Sunny" ofType:@"aac"]];
player = [[AVAudioPlayer alloc]initWithContentsOfURL:FileURL error:nil];
//[changecolor setAlpha:0.0];
[campFireView setAlpha:0.1];
[bgview setAlpha:1];
[self.view addSubview:toolbar];
}
我的应用程序即使在处理了每个指针alloc
??
jus想知道这种方法是否正确?
答案 0 :(得分:2)
viewDidLoad
。例如,当您显示模态视图控制器时,可以卸载下面的视图,当模态被解除时,将调用viewDidLoad
。
因此,如果您在viewDidLoad
中分配对象,则可能会泄露它们。这就是你应该在viewDidUnload
中发布它们的原因。在你的情况下:
- (void)viewDidUnLoad {
[b1 release], b1 = nil;
[b2 release], b2 = nil;
[b3 release], b3 = nil;
[b4 release], b4 = nil;
[b5 release], b5 = nil;
[b6 release], b6 = nil;
[b7 release], b7 = nil;
[s1 release], s1 = nil;
[s2 release], s2 = nil;
[s3 release], s3 = nil;
[s4 release], s4 = nil;
[s5 release], s5 = nil;
[s6 release], s6 = nil;
[s7 release], s7 = nil;
[bg release], bg = nil;
[bgview release], bgview = nil;
[player release], player = nil;
[super viewDidUnload];
}
答案 1 :(得分:0)
在加载viewController时只调用一次ViewDidLoad。刷新视图时会多次调用viewWillAppear。
答案 2 :(得分:0)
尝试浏览Apple的Memory Management Programming Guide。