2010-01-15 35 views
2

我一直在玩一個簡單的iphone應用程序,並決定把NSLog語句放在控制器和委託的dealloc中,但是這些都不打印到Xcode控制檯上?iPhone委託和控制器dealloc?

//應用程序委託

#import "iPhone_buttonFunAppDelegate.h" 
#import "iPhone_buttonFunViewController.h" 

@implementation iPhone_buttonFunAppDelegate 

@synthesize window; 
@synthesize viewController; 

- (void)applicationDidFinishLaunching:(UIApplication *)application {  
    // Override point for customization after app launch 
    NSLog(@"applicationDidFinishLaunching ..."); 
    [window addSubview:viewController.view]; 
    [window makeKeyAndVisible]; 
} 

- (void)applicationWillTerminate:(UIApplication *)application { 
    NSLog(@"AWT:"); 
} 

- (void)dealloc { 
    NSLog(@"-DEL-"); // << Does not print? 
    [viewController release]; 
    [window release]; 
    [super dealloc]; 
} 
@end 

//視圖控制器

#import "iPhone_buttonFunViewController.h" 

@implementation iPhone_buttonFunViewController 
@synthesize statusText; 

-(IBAction)buttonPressed:(id) sender { 
    NSString *title; 
    NSString *newText; 

    title = [sender titleForState:UIControlStateNormal]; 
    newText = [[NSString alloc] initWithFormat:@"%@ button pressed.", title]; 
    [statusText setText:newText]; 
    [newText release]; 
    NSLog(@"Button Press ... %@", title); 
} 

-(void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 
    // Release any cached data, images, etc that aren't in use. 
    NSLog(@"-1-"); 
} 

-(void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
    NSLog(@"-2-"); 
    self.statusText = nil; 
} 

-(void)dealloc { 
    NSLog(@"-CON-"); // << Does not print? 
    [statusText release]; 
    [super dealloc]; 
} 
@end 

加里

回答

5

這是Cocoa touch運行時的優化。在程序結束時不會執行某些釋放操作,因爲整個程序將會退出,無論如何它們將被清除。

+0

因此,基本上我在那裏發佈的所有版本都沒有完成,清理本質上是應用程序正在退出並且其足跡將被清理的事實? – fuzzygoat 2010-01-17 22:35:34

+1

正確。在正常的程序操作中從不調用AppDelegate的dealloc方法。 – 2010-01-22 03:01:41

0

此問題的NSLog(...)可以通過this other stackoverflow question about applicationWillTerminate:

好回答運氣。

+0

謝謝,可能是iPhone_buttonFunAppDelegate的dealloc(並因此導致iPhone_buttonFunViewController dealloc)在applicationWillTerminate後被調用。在這種情況下,不會輸出更多的NSLog。 – fuzzygoat 2010-01-15 23:25:07

+0

是的,deallocs通常在那之後。控制檯是否有你的NSLog(...)? – corprew 2010-01-15 23:36:05

+0

當您按下模擬器主頁按鈕時,不在系統控制檯中顯示的最後一件事是來自applicationWillTerminate的NSLog。雖然這是模擬器和Xcode之間的連接被中斷的地方,但重新進入應用程序會導致NSLog只進入系統控制檯,但是您仍然看不到來自任何dealloc的NSLog。 – fuzzygoat 2010-01-15 23:57:52