iPhone代表&控制器dealloc?

时间:2010-01-15 22:00:55

标签: objective-c cocoa-touch

我一直在玩一个简单的iphone应用程序,并决定在控制器和委托的deallocs中放置一个NSLog语句,但是这些都没有打印到Xcode控制台?

// APPLICATION 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

//查看控制器

#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

加里

2 个答案:

答案 0 :(得分:5)

这是Cocoa touch运行时的优化。在程序结束时不会进行某些解除分配,因为整个程序将要退出,无论如何它们都将被清除。

答案 1 :(得分:0)

NSLog(...)的这个问题可以通过this other stackoverflow question about applicationWillTerminate:

来回答 祝你好运。