2013-08-12 107 views
4

你好朋友我在創建MAC應用程序時遇到問題我有兩個視圖控制器。當我點擊按鈕被放置在第一視圖控制器應用程序代替圖使用以下代碼按鈕點擊事件MAC應用程序崩潰

- (IBAction)gotoEmployeeView:(id)sender 
{ 
    delegate = (AppDelegate *)[[NSApplication sharedApplication]delegate]; 
    secondViewController *employeeVC = [[secondViewController alloc]initWithNibName:@"secondViewController" bundle:nil]; 
    [[delegate.window.contentView animator] replaceSubview:self.view with:secondVC.view]; 
    secondVC.view.frame = ((NSView *)delegate.window.contentView).bounds; 
} 

現在我有一個按鈕,我已經設置點擊事件有關第二視圖此按鈕。下面的id按鈕點擊事件

- (IBAction)btnClicked:(id)sender 
{ 
    NSLog(@"clicked"); 
} 

當我點擊它時,應用程序崩潰。並得到以下錯誤

2013-08-12 14:33:46.989 Employee Register[1954:303] -[__NSCFString btnClicked:]: unrecognized selector sent to instance 0x10213ace0 
2013-08-12 14:33:46.991 Employee Register[1954:303] -[__NSCFString btnClicked:]: unrecognized selector sent to instance 0x10213ace0 
2013-08-12 14:33:46.994 Employee Register[1954:303] (
0 CoreFoundation      0x00007fff8d76cf56 __exceptionPreprocess + 198 
1 libobjc.A.dylib      0x00007fff80e47d5e objc_exception_throw + 43 
2 CoreFoundation      0x00007fff8d7f91be -[NSObject doesNotRecognizeSelector:] + 190 
3 CoreFoundation      0x00007fff8d759e23 ___forwarding___ + 371 
4 CoreFoundation      0x00007fff8d759c38 _CF_forwarding_prep_0 + 232 
5 CoreFoundation      0x00007fff8d75c70d -[NSObject performSelector:withObject:] + 61 
6 AppKit        0x00007fff8c5208ca -[NSApplication sendAction:to:from:] + 139 
7 AppKit        0x00007fff8c5207fe -[NSControl sendAction:to:] + 88 
8 AppKit        0x00007fff8c520729 -[NSCell _sendActionFrom:] + 137 
9 AppKit        0x00007fff8c51fbec -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2014 
10 AppKit        0x00007fff8c59fb74 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 489 
11 AppKit        0x00007fff8c51e7f6 -[NSControl mouseDown:] + 786 
12 AppKit        0x00007fff8c4e9c98 -[NSWindow sendEvent:] + 6306 
13 AppKit        0x00007fff8c4833a5 -[NSApplication sendEvent:] + 5593 
14 AppKit        0x00007fff8c419a0e -[NSApplication run] + 555 
15 AppKit        0x00007fff8c695eac NSApplicationMain + 867 
16 Employee Register     0x0000000100001342 main + 34 
17 Employee Register     0x0000000100001314 start + 52 
) 

回答

0

它看起來像你發送一個btnClicked消息到一個NSString對象。你可以在你打電話給[... bntClicked]的地方發佈代碼嗎?

您正在描述的行爲可能意味着實現btnClicked方法的viewController在調用操作之前被取消分配,或者界面構建器中的操作未正確設置。

您應該在實現btnClicked的viewController的dealloc方法中添加一條日誌語句,並且在調用動作之前看到它不會被釋放。如果您使用的是ARC,請確保您的成員數據指向您的視圖控制器,以免它們被釋放。

此外,您應該刪除界面構建器中的ibaction關聯,並再次執行操作,確保將按鈕連接到正確的視圖控制器。

0

您的gotoEmployeeView代碼對我來說看起來很好。
注意replaceSubview:with:方法會導致舊視圖被釋放。

看看sample project

+0

你好帕拉格感謝您的回答,但我的應用程序崩潰按鈕單擊事件 – Pratik

+0

檢查我的示例項目。 –

+0

我已經複製你的代碼,並把我的項目,但沒有運氣 – Pratik

相關問題