objective-c - iPhone 委托(delegate)和 Controller 解除分配?

标签 objective-c cocoa-touch

我一直在玩一个简单的 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/

相关文章:

ios - GPPSignIn 委托(delegate)方法未被执行?

iphone - 为什么要写[anView release], anView = nil;而不是 [anView 发布];?

iphone - iOS 5,ARC : UIAlertView doesn't work in @catch block

objective-c - Cocoa 中的自定义字体错位

objective-c - NSURLSessionDataTask 运行缓慢

objective-c - 读取 9 位、10 位或 11 位(精确) block 中的数据并在 Objective-C/C 中转换为整数

objective-c - NSString 地址问题

iphone - 通过循环将 TouchUpInside 事件分配给多个 UIImageView,并使用可以进一步对每个 UIImageView 进行动画/操作的处理程序方法

ios - 将属性从模态视图 Controller 传递给父级

iphone - 当 UIImageView.image 设置 2-3 次时应用程序崩溃