2010-01-15 36 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