【发布时间】:2012-07-28 11:16:49
【问题描述】:
我正在尝试解决一个更大的问题,并且我认为 ARC 显然过早地向我的 NSViewController 发布了视图。我认为 :) 所以我创建了一个简单的应用程序来重建情况。
我有一个简单的 ARC Cocoa 应用程序。在MainMenu.xib 的窗口中,我将Custom View 连接到@property (strong) IBOutlet NSView *theView;,它在AppDelegate.h 中声明
在AppDelegate.m我合成属性然后调用如下:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
TestViewController *tvc = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];
[_theView addSubview:[tvc view]];
}
TestViewController 显示在 Custom View 中 - 没问题。它包含一个 NSButton。它连接到一个名为 -(IBAction)btnPressed:(id)sender 的方法和一个也连接为 IBOutlet 的 NSTextView。
在TestViewController.h我声明:
@property (nonatomic, strong) IBOutlet NSTextField *textField;
@property (nonatomic, strong) NSString *theString;
-(IBAction)btnPressed:(id)sender;
在TestViewController.m我然后做
@synthesize theString = _theString;
@synthesize textField = _textField;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
_theString = @"Hello World";
}
return self;
}
-(IBAction)btnPressed:(id)sender
{
[_textField setStringValue:_theString];
}
当我运行应用程序并按下按钮时,它会崩溃。如果我检查它是否有僵尸,我会收到以下信息:
# Address Category Event Type RefCt Timestamp Size Responsible Library Responsible Caller
0 0x7f97a3047560 TestViewController Malloc 1 00:00.652.631 128 TestARC -[AppDelegate applicationDidFinishLaunching:]
1 0x7f97a3047560 TestViewController Retain 2 00:00.653.088 0 TestARC -[TestViewController initWithNibName:bundle:]
2 0x7f97a3047560 TestViewController Release 1 00:00.653.089 0 TestARC -[TestViewController initWithNibName:bundle:]
3 0x7f97a3047560 TestViewController Retain 2 00:00.653.912 0 AppKit -[NSNib instantiateNibWithOwner:topLevelObjects:]
4 0x7f97a3047560 TestViewController Release 1 00:00.658.831 0 AppKit -[NSNib instantiateNibWithOwner:topLevelObjects:]
5 0x7f97a3047560 TestViewController Release 0 00:00.662.377 0 Foundation -[NSNotificationCenter postNotificationName:object:userInfo:]
6 0x7f97a3047560 TestViewController Zombie -1 00:01.951.377 0 AppKit -[NSApplication sendAction:to:from:]
我做错了什么? 谢谢
【问题讨论】:
标签: objective-c cocoa automatic-ref-counting