在按钮上单击“无法识别的选择器发送到实例”

时间:2014-10-14 13:22:26

标签: objective-c macos cocoa nsbutton nsviewcontroller

Cocoa / Objective-C新手在这里。

我有一个MainMenu.xib文件,其中包含NSView。 这是AppDelegate.h中awakeFromNib的样子:

- (void)awakeFromNib {
NSViewController *x = [[Login alloc] initWithNibName:@"Login" bundle:nil];
NSView *v = [x view];
[_theView addSubview:v];    

}

新视图有一个按钮但是当我点击它时,我收到消息“无法识别的选择器发送到实例”。

我完全迷失了。请帮我解决一下这个。谢谢!

它也会在行(在main.m中)中以“线程1:EXC_BAD_ACCESS(code = 1,address = ...)”的形式抛出:

return NSApplicationMain(argc, argv);

错误是:

2014-10-14 09:05:09.743 FAST Tax Scanner [18408:303] - [OS_dispatch_queue_runloop login:]:无法识别的选择器发送到实例0x6000000f7900 2014-10-14 09:05:09.743 FAST Tax Scanner [18408:303] - [OS_dispatch_queue_runloop login:]:无法识别的选择器发送到实例0x6000000f7900 2014-10-14 09:05:09.744快速税务扫描[18408:303](     0 CoreFoundation 0x00007fff9484725c exceptionPreprocess + 172     1 libobjc.A.dylib 0x00007fff8c72fe75 objc_exception_throw + 43     2 CoreFoundation 0x00007fff9484a12d - [NSObject(NSObject)doesNotRecognizeSelector:] + 205     3 CoreFoundation 0x00007fff947a5272 ___ forwarding _ + 1010     4 CoreFoundation 0x00007fff947a4df8 _CF_forwarding_prep_0 + 120     5 AppKit 0x00007fff8b87d260 - [NSApplication sendAction:to:from:] + 327     6 AppKit 0x00007fff8b87d0de - [NSControl sendAction:to:] + 86     7 AppKit 0x00007fff8b8c9c4d - [NSCell _sendActionFrom:] + 128     8 AppKit 0x00007fff8b8e3655 - [NSCell tr​​ackMouse:inRect:ofView:untilMouseUp:] + 2316     9 AppKit 0x00007fff8b8e2a27 - [NSButtonCell tr​​ackMouse:inRect:ofView:untilMouseUp:] + 487     10 AppKit 0x00007fff8b8e213d - [NSControl mouseDown:] + 706     11 AppKit 0x00007fff8b863a58 - [NSWindow sendEvent:] + 11296     12 AppKit 0x00007fff8b8025d4 - [NSApplication sendEvent:] + 2021     13 AppKit 0x00007fff8b6529f9 - [NSApplication run] + 646     14 AppKit 0x00007fff8b63d783 NSApplicationMain + 940     15快速税务扫描仪0x0000000100001452主要+ 34     16 libdyld.dylib 0x00007fff982c55fd start + 1     17 ??? 0x0000000000000003 0x0 + 3 )

1 个答案:

答案 0 :(得分:1)

哦,我明白了....

尝试将NSViewController *x保存为创建它的对象的强属性,而不是将其作为局部变量。它当前创建的方式,Login对象将无法在awakeFromNib方法结束后继续存在。