2011-01-04 15 views
0

Ich habe gehört, dass viewDidLoad() Methode mehrmals während der Ausführung des Codes aufgerufen wird..so wenn ich alloc einige Ansichten in dieser Methode, Es wird eine neue pointer jedes Mal erstellen, wenn es aufgerufen wird, ohne sie zu veröffentlichen.Abfragen über ViewDidLoad

hier ist meine Methode

-(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]; 
} 

meine App noch stürzen auch nach jedem Zeiger i alloc kümmert ??

jus will wissen, ob dieser Ansatz richtig ist oder nicht ??

+1

Ihre Syntax ist für eine objective-c-Methode deaktiviert :) Es sollte '- (void) viewDidLoad' sein; nicht '- (void) viewDidLoad()'; das wäre eine Mischung aus C und Obj-C, die es nicht gibt. –

Antwort

2

viewDidLoad kann tatsächlich mehrmals aufgerufen werden, wenn das System die Ansicht entladen muss (wahrscheinlich für Speicherzweck). Wenn Sie beispielsweise einen modalen Ansichtscontroller anzeigen, wird möglicherweise die folgende Ansicht entladen und wenn das Modal abgewiesen wird, wird viewDidLoad aufgerufen.

Wenn Sie also Objekte in viewDidLoad zuweisen, können Sie sie lecken. Deshalb sollten Sie sie in viewDidUnload freigeben. In Ihrem Fall:

- (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]; 
} 
+0

gibt es eine andere Möglichkeit, diese Technik zu verwenden ??? die Autorelease ist nicht Freigabe der Speicher auf, wenn es benötigt ..ie stürzt die App .. :( – Hisenberg

+0

Ich würde beginnen, indem Speicherlecks mit der Option "Build and Analyse" und Instrumente ... Überprüfen Sie die [Memory Management-Programmierhandbuch] (http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html) Wenn Ihre App wegen zu wenig Arbeitsspeicher abstürzt, werden wahrscheinlich Speicherwarnungen ausgegeben Zuvor erhalten Ihre View-Controller und -App-Delegaten ein 'didReceiveMemoryWarning'. Bei dieser Methode sollten Sie Speicherobjekte freigeben, die Sie später problemlos neu erstellen können. – Jilouc

0

ViewDidLoad wird nur einmal aufgerufen, wenn der viewController geladen ist. ViewWillAppear wird mehrmals aufgerufen, wenn die Ansicht aktualisiert wird.

+0

-viewDidLoad wird jedes Mal aufgerufen, wenn die Ansicht geladen wird, aber ein einzelner UIViewController kann seine Ansicht oft laden und entladen. – Jonah