2010-02-20 27 views
2

我正在處理我的第一個真正的iPhone應用程序,一個簡單的待辦事項列表應用程序來幫助我組織東西,除了我得到一個「無法識別的選擇器發送到實例0x「。iPhone:IBAction導致「無法識別的選擇器發送到實例」錯誤

具體做法是:

2010-02-20 14:30:09.200 ToDoApp[88562:20b] *** -[NSCFDictionary switchViews:]: unrecognized selector sent to instance 0x3d22de0 

2010-02-20 14:30:09.201 ToDoApp[88562:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFDictionary switchViews:]: unrecognized selector sent to instance 0x3d22de0' 

我環顧四周,並想通了,它可能是在IB的連接問題,但我是新來的這整個事情連接(男人,我希望他們支持的Java或Python),所以這是它的佈局。我有3個類,一個SwitchViewController,一個MainScreenViewController和一個ToDoListViewController。當我點擊MainScreenViewController上的一個按鈕時,我觸發了引發這個問題的「switchViews」函數。他們建立的方式是,按鈕(一個UIBarButtonItem)具有「sentAction」去switchViews。這個ViewButton在SwitchViewController中有其作爲IBOutlet的參考插座。

因此,這裏的.H爲SVC:

#import <UIKit/UIKit.h> 

@class MainScreenViewController; 
@class ToDoListViewController; 
@class EditViewController; 

#define kMinimumGestureLength 25 
#define kMaximumVariance 5 

@interface SwitchViewController : UIViewController { 
MainScreenViewController *mainScreenViewController; 
ToDoListViewController *toDoListViewController; 
EditViewController *editViewController; 
IBOutlet UIBarButtonItem *viewButton; 
CGPoint gestureStartPoint; 
} 

@property (retain, nonatomic) MainScreenViewController *mainScreenViewController; 
@property (retain, nonatomic) ToDoListViewController *toDoListViewController; 
@property (retain, nonatomic) EditViewController *editViewController; 
@property (retain, nonatomic) IBOutlet UIBarButtonItem *viewButton; 
@property CGPoint gestureStartPoint; 
-(IBAction)switchViews:(id)sender; 

而對於switchViews功能:

-(IBAction) switchViews:(id)sender 
{ 
NSLog(@"Switching views"); 
if(self.toDoListViewController.view.superview == nil){ 
    if(self.toDoListViewController ==nil){ 
    ToDoListViewController *toDoVC = [[ToDoListViewController alloc]  initWithNibName:@"ToDoListView" bundle:nil]; 
    self.toDoListViewController = toDoVC; 
    //[toDoVC release]; 
    } 
    [mainScreenViewController.view removeFromSuperview]; 
    [self.view insertSubview:toDoListViewController.view atIndex:0]; 
} 
else{ 
    if(self.mainScreenViewController == nil){ 
    MainScreenViewController *mainController =  [[MainScreenViewController alloc] initWithNibName:@"MainScreenView" bundle:nil]; 
    self.mainScreenViewController = mainController; 
    //[mainController release]; 
    } 
    [toDoListViewController.view removeFromSuperview]; 
    [self.view insertSubview:mainScreenViewController.view atIndex:0]; 
} 
} 

因此,在短期,我完全失去了,這真是令人沮喪。任何人有任何建議,或需要更多的代碼?

回答

4

好的,得到了​​解決方案指出。應該有它通過FirstResponder路由(我...真的不明白爲什麼那個作品,但在這一點上,我只是很高興它的作品。)

我不知道第一響應者如何工作反正(無我真的提到它的書),但它......有效嗎?如果有人想給我一個簡歷,那會很有用......但這個問題已經得到解答。

+0

具有完全相同的問題,在我通過FirstResponder路由後修復(但仍不知道爲什麼會起作用) – jbnunn 2010-02-27 16:32:12

0

我要猜測問題出在你的筆尖文件中。

該錯誤意味着單擊該按鈕時,該按鈕會嘗試將一個消息/方法調用switchView發送給NSDictionary對象,該對象當然沒有此類方法。這個錯誤就存在於按鈕動作指向的地方。

檢查此筆尖的筆尖。查看文件所有者並檢查分配給它的類。確保它是SwitchViewController而不是字典出於某種原因。如果文件所有者屬性設置爲字典類,則它將加載字典並嘗試將其操作方法發送給它。

+0

分配給它的類是「MainScreenViewController」,它或多或少是一個空視圖控制器類,它被設計爲當其中一個按鈕被擊中時作出反應。我在IB的MainScreenView中將SwitchViewController作爲外部對象,並將IBOutlet viewButton連接到MainScreenView(它被視爲SwitchViewController視圖的子視圖)中的UIBBI。如果這有助於任何。 – Althane 2010-02-21 18:59:08

+0

這是正確答案?如果是這樣,你需要檢查標記。否則系統會記錄這個未答覆的問題。 – TechZen 2010-02-22 12:40:17

12

我們剛碰到同樣的問題。看起來我們在AppDelegate中發佈了ViewController對象,然後我們的nib視圖試圖調用一個IBAction(在視圖控制器上)。有一半的時間我們得到了「EXC_BAD_ACCESS」(又名消息發佈對象),還有一半是我們得到NSCFString,NSCFArray的「無法識別的選擇器發送給實例」的時間,各種各樣的事情(也就是傳遞一個記憶區域由不同的對象)。

只要檢查你的ViewController尚未發佈。

+1

優秀的答案。 – mydogisbox 2012-03-22 18:46:27

+0

謝謝,這只是爲我節省了很多猜測...... – jd291 2012-12-06 22:39:07

+0

來到這裏報告這一點。我遇到了各種對象的奇怪的不良訪問崩潰。這是我有一個記憶問題的第一個建議。本來我以爲我忘了設置正確的文件的所有者。我偶然在viewDidLoad方法中實例化了一個視圖控制器,在調用IBAction的時候它是零! – anders 2015-07-08 15:41:40

0

這可能也有幫助。分析器例程建議我釋放一些對象,而我做了。事實證明,我需要在應用程序中的這些對象。在xAppDelegate.m文件(或其他)在appDidFinishLaunching方法/消息/功能/例程/東西,使用

UINavigationController *navController = [[UINavigationController alloc] init]; 

代替

UINavigationController *navController = [[[UINavigationController alloc] init] autorelease]; 

此外,分析器推薦我釋放我推到物體導航控制器。大錯。該文件是我的菜單屏幕,當我按下按鈕時,我收到了一個unrecognized selector sent to instance。顯然,它在NSStringNSDictionary上調用IBAction,這並不好。

0

正確的答案是這樣的:

,我們指定作爲應用程序代理的第一個屏幕視圖控制器不應該在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法來釋放。在你的情況下,第一個屏幕是MainScreenViewController

它(MainScreenViewController實例)應該在應用代理的dealloc方法中發佈。

- (void)dealloc 
{ 
    [_window release]; 
    [mainScreenViewController release]; 
    [super dealloc]; 
} 
0

僅供參考使用我ARC時漸漸這和廈門國際銀行正在加載,並把在屏幕上,但不知何故VC本身沒有被保留。

我通過添加一個變量來存儲引用它的VC中的引用來解決它。

0

問題是,您已經啓動了UIViewController實例作爲方法變量。因此,視圖控制器在執行該方法後沒有範圍,因此它從內存循環中釋放。所以你必須將你的視圖控制器實例設置爲類級別。

@interface SwitchViewController() { 
     ToDoListViewController *toDoVC; 
     MainScreenViewController *mainController; 
    } 



-(IBAction) switchViews:(id)sender 
{ 
    if (!toDoVC) 
      toDoVC = [[ToDoListViewController alloc]  initWithNibName:@"ToDoListView" bundle:nil]; 
    if (!mainController) 
      mainController =  [[MainScreenViewController alloc] initWithNibName:@"MainScreenView" bundle:nil]; 

//Your stuff with the view controllers... 
} 
相關問題