[英]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'
Touch Up Inside使用buttonPressed鏈接到文件的所有者。
已下載其他示例代碼,它們工作正常。 我的項目和筆尖的設置與其他示例代碼完全相同。
還沒有找到哪里開始嘗試調試它的線索。
有人幫嗎?
我有同樣的問題。 這是因為您的ViewController位於TabBarController下,並且在其下的View Controller中,您將NIB名稱設置為Contact,但未在標識窗格中將Class設置為Contact。
在Interface Builder中,您需要告訴它File類的所有者是什么類。 單擊“文件所有者”圖標,轉到“檢查器”中的“身份”窗格(末尾的“i”圖標),然后將“類”更改為“聯系人”。
你確定你粘貼了正確的代碼嗎? 您的Contact.m應該包含一個包含實現的@implementation
塊,而不是包含定義的@interface
塊
Contact.m:
#import "Contact.h"
@implementation Contact
- (IBAction)buttonPressed:(id)sender
{
NSLog(@"buttonPressed");
}
@end
剛創建了一個全新的項目,除了UIView和按鈕之外什么都沒有,它仍然在發生
這讓我覺得你並沒有很好地正確設置。 如果這是應用程序nib文件,並且它只包含UIView
,則文件所有者將成為您的應用程序委托,無論您將文件所有者的類型設置為IB。
請嘗試以下方法:
在您的nib文件中創建一個Contact
實例:在UIViewController
對象中拖動,並將其類設置為Contact
。
將視圖和按鈕連接到Contact
實例。
如果有效,請告訴我們:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.