我一直在玩一个简单的 iPhone 应用程序,并决定在 Controller 和委托(delegate)的 dealloc 中放入 NSLog 语句,但这些语句都没有打印到 Xcode 控制台?
//应用程序委托(delegate)
#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
//查看 Controller
#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
加里
最佳答案
这是 Cocoa touch 运行时的优化。某些释放不会在程序结束时进行,因为整个程序将退出并且它们无论如何都会被清除。
关于objective-c - iPhone 委托(delegate)和 Controller 解除分配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2075069/