2009-08-31 61 views
3

我在這裏撕掉我的頭髮。閱讀了很多描述如何解決這個問題的例子,但沒有人幫助我擺脫了這個問題。NSInvalidArgumentException

在UIView鏈接到IBAction上有一個簡單的按鈕。

代碼是這樣的......

Contact.h

#import <UIKit/UIKit.h> 

@interface Contact : UIViewController { 
} 

-(IBAction)buttonPressed:(id)sender; 

@end 

Contact.m

#import "Contact.h" 

@implementation Contact 

- (IBAction)buttonPressed:(id)sender 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Button Pressed" message:@"You pressed the button" delegate:nil cancelButtonTitle:@"Yep, I did." otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
} 

不斷收到此錯誤信息:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIViewController buttonPressed:]: unrecognized selector sent to instance 0xd1d5f0' 

觸摸的內心鏈接到該文件所有者使用buttonPressed。

已經下載了其他示例代碼,它們工作正常。我的項目和筆尖的設置與其他示例代碼完全相同。

還沒有一個線索,甚至開始嘗試和調試。

任何幫助?

+0

代碼看起來像什麼來建立TouchUp事件回調? – fbrereto 2009-08-31 18:01:17

回答

5

我有此相同的問題。這是因爲您的ViewController位於TabBarController下,並且在其下的View Controller中,您將NIB名稱設置爲聯繫人,但未在身份窗格中將類設置爲聯繫人。

+1

是的,這是它:) – 2009-09-04 20:46:04

+0

同樣的問題。我希望我能投票更多。 – Dave 2011-03-13 00:31:14

0

你確定你粘貼了正確的代碼嗎?你Contact.m應該包含含有實施@implementation塊,包含定義

Contact.m不是@interface塊:

#import "Contact.h" 

@implementation Contact 

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

@end 
+0

哎呀,你是對的。現在已經粘貼它,因爲它應該是:) – 2009-08-31 18:02:34

+0

上午也構建一個全新的項目從一個按鈕和一個IBAction,沒有其他代碼,它仍然給我同樣的頭痛:( – 2009-08-31 18:03:20

1

在Interface Builder中,你需要告訴它什麼類文件的所有者是。單擊「文件所有者」圖標,轉到檢查器中的「身份」窗格(最後的「我」圖標),然後將類更改爲聯繫人。

+0

嗨,我已經做到了。另外,只是創建了一個全新的項目,除了UIView和一個按鈕之外,它裏面沒有任何東西,它仍然在發生:( – 2009-08-31 18:09:12

+0

你確定*?這部分錯誤:''*** - [UIViewController buttonPressed: ]:無法識別的選擇器發送到實例0xd1d5f0''告訴你,一個UIViewController實例正在接收消息,而不是一個聯繫人實例 – iKenndac 2009-08-31 18:13:12

+0

+1我認爲這是正確的答案 – 2009-08-31 18:31:37

0

just created a brand new project with nothing inside it other than a UIView and a button and it is still happening

這讓我覺得你並沒有很好地設置。如果這是應用程序nib文件,並且它只包含UIView,則文件的所有者將成爲您的應用程序委託,而不管您將文件所有者的類型設置爲IB。

嘗試以下操作:

  1. 在你的筆尖文件中創建的Contact一個實例:在UIViewController對象拖動和它的類設置爲Contact

  2. 連線視圖和按鈕直到您的Contact實例。

  3. 讓我們知道,如果工程:)

+0

創建一個新的項目,它工作正常。認爲這可能是由於它是從TabBarController加載的視圖這一事實。 NIB本身是使用正確的IBAction和類,但我似乎無法確定'無法識別的選擇器'被髮送到哪裏。 感謝所有人的幫助,我會士兵,直到我發現它是我做錯了什麼:) – 2009-08-31 23:50:25