2013-07-23 139 views
2

我來自iOS開發背景。我在appdelegate中創建簡單的mac應用程序我將NSViewController實例添加到窗口,並且我在NSViewController類中有一個按鈕。當我點擊按鈕時,它崩潰。我看着這些帖子,但我無法弄清楚我的應用出了什麼問題。 Why would my app not be able to "attach" an action?點擊按鈕時應用程序崩潰

這裏是按鈕

-(IBAction)startClicked:(id)sender{[email protected]"Btn selected";} 

當我按一下按鈕,應用程序崩潰的應用程序委託

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

函數的代碼。 以下是堆棧跟蹤:

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 

任何幫助將感激 感謝

+1

什麼是錯誤?什麼線路崩潰。 – utahwithak

+0

什麼是堆棧跟蹤呢? – BergQuester

+0

當我點擊按鈕時它崩潰。 – pa12

回答

6

我覺得視圖控制器被釋放過早,因爲你沒有持有對它的引用:

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

您需要使用視圖使pcVC的類的實例變量或者更好的是動搖你的iOS背景並完全停止使用視圖控制器,以支持從NIB直接加載視圖。

這個article給了我關於它與內存管理有關的想法。

+0

謝謝。像魅力一樣工作。不知道我是如何錯過這個基本的。 – pa12

0

在Interface Builder打開控制器並查看其行動(右鍵點擊橙色圓圈)。看起來您的行動鏈接已損壞。這可能是操作重命名,或刪除然後再添加方法的結果。

+0

我檢查它,但行動似乎是完好的。 – pa12

相關問題