Macos 单击按钮时应用程序崩溃

Macos 单击按钮时应用程序崩溃,macos,Macos,我来自iOS开发背景。我正在appdelegate中创建简单的mac应用程序我正在将NSViewController实例添加到窗口中,并且在NSViewController类中有一个按钮。当我点击按钮时,它崩溃了。我看了这些帖子,但我不知道我的应用程序出了什么问题。 下面是应用程序委托中的代码 PrViewController *pcVC = [[PrViewController alloc] initWithNibName:@"PrViewController" bundle:nil]; [

我来自iOS开发背景。我正在appdelegate中创建简单的mac应用程序我正在将NSViewController实例添加到窗口中,并且在NSViewController类中有一个按钮。当我点击按钮时,它崩溃了。我看了这些帖子,但我不知道我的应用程序出了什么问题。

下面是应用程序委托中的代码

PrViewController *pcVC = [[PrViewController alloc] initWithNibName:@"PrViewController" bundle:nil];
[self.window.contentView addSubview:pcVC.view];
按钮的功能

    -(IBAction)startClicked:(id)sender{NSLog@"Btn selected";}
当我点击按钮时,应用程序崩溃。 以下是堆栈跟踪:

Producer_consumer[45914:303] -[__NSCFType startClicked:]: unrecognized selector sent to instance 0x101d07680
 Producer_consumer[45914:303] -[__NSCFType startClicked:]: unrecognized selector sent to instance 0x101d07680
Producer_consumer[45914:303] (
0   CoreFoundation                      0x00007fff900deb06 __exceptionPreprocess + 198
1   libobjc.A.dylib                     0x00007fff8ce803f0 objc_exception_throw + 43
2   CoreFoundation                      0x00007fff9017540a -[NSObject(NSObject) doesNotRecognizeSelector:] + 186
3   CoreFoundation                      0x00007fff900cd02e ___forwarding___ + 414
4   CoreFoundation                      0x00007fff900cce18 _CF_forwarding_prep_0 + 232
5   AppKit                              0x00007fff85862959 -[NSApplication sendAction:to:from:] + 342
6   AppKit                              0x00007fff858627b7 -[NSControl sendAction:to:] + 85
7   AppKit                              0x00007fff858626eb -[NSCell _sendActionFrom:] + 138
8   AppKit                              0x00007fff85860bd3 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 1855
9   AppKit                              0x00007fff85860421 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 504
10  AppKit                              0x00007fff8585fb9c -[NSControl mouseDown:] + 820
11  AppKit                              0x00007fff8585750e -[NSWindow sendEvent:] + 6853
12  AppKit                              0x00007fff85853644 -[NSApplication sendEvent:] + 5761
13  AppKit                              0x00007fff8576921a -[NSApplication run] + 636
14  AppKit                              0x00007fff8570dbd6 NSApplicationMain + 869
15  Producer_consumer                   0x0000000100001c02 main + 34
16  libdyld.dylib                       0x00007fff920127e1 start + 0
)

任何帮助都将不胜感激
谢谢

在Interface Builder中打开控制器并查看其操作(右键单击橙色圆圈)。看起来你的动作链接断了。这可能是操作重命名、删除然后再次添加方法的结果。

我认为视图控制器被过早释放,因为您没有持有对它的引用:

PrViewController *pcVC = [[PrViewController alloc] initWithNibName:@"PrViewController" bundle:nil];
[self.window.contentView addSubview:pcVC.view];
您需要使
pcVC
成为使用视图的类的实例变量,或者最好还是摇一下iOS背景,完全停止使用视图控制器,以便直接从NIB加载视图


这让我想到它与内存管理相关。

错误是什么?它在哪一行崩溃。堆栈跟踪也是什么?当我点击按钮时,它崩溃了。我检查了它,但动作似乎完好无损。谢谢。工作得很有魅力。不知道我怎么会错过这个基本的。