我正在尝试解决一个更大的问题,并且我认为 ARC 显然过早地向我的 NSViewController 发布了视图。我认为 :) 所以我创建了一个简单的应用程序来重建这种情况。
我有一个简单的 ARC Cocoa 应用程序。在MainMenu.xib
I 的窗口中,将 aCustom 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
和一个 NSTextView,该方法也连接为IBOutlet
.
在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:]
我究竟做错了什么?谢谢