Search code examples
iphoneobjective-cxcodecocoaibaction

Xcode: Button clicked results in NSInvalidArgumentException


I'm working in Xcode 4.3.2.

I am new to Xcode. I'm building an application that must change to different views on button clicks. My files are: AppDelegate.h/.m, GreenViewController.h/.m, SwitchViewController.h/.m, GreenView.xib - I am not using storyboards, but my project demands that I don't use them (backwards compatibility issues).

Here's my problem (it seems very simple): I'm trying to print to the console when a UIButton (placed in GreenView.xib) is clicked. Here's my code for GreenViewController.h

#import <UIKit/UIKit.h>
@interface GreenViewController : UIViewController
- (IBAction)switchViews:(id)sender;
@end

Here's my (deprecated) code for GreenViewController.m:

#import "GreenViewController.h"
@implementation GreenViewController

- (IBAction) switchViews:(id)sender {
    NSLog(@"Button Pressed!");
}

The owner for GreenView.xib is GreenViewController.

For some reason I have the error only when the UIButton (in GreenView.xib) is pressed:

2012-10-09 18:07:38.490 MyViewSwitcher[8655:f803] -[SwitchViewController switchViews:]: unrecognized selector sent to instance 0x688a660
2012-10-09 18:07:38.492 MyViewSwitcher[8655:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SwitchViewController switchViews:]: unrecognized selector sent to instance 0x688a660'

It seems that SwitchViewController is expecting something from the method "switchViews", but "switchViews" is only listed in GreenViewController. Before, I had "switchViews" in SwitchViewController, but I deleted all the code corresponding to the method & all the connections. Again, I've double checked that "switchViews" in GreenViewController is connected to the UIButton found in GreenView.xib. I've already cleaned & re-built my project & I still get this error.

Thanks for your help!


Solution

  • What your error is saying is that you are calling the switchViews: method on an instance of SwitchViewController. And since there is no definition for switchViews: (because you deleted it) for the class SwitchViewController, it doesn't know what to do, and crashes.

    Hate to tell ya this, but your button is connected to the switchViews: method of a SwitchViewController. You say "I've double checked that 'switchViews' in GreenViewController is connected to the UIButton found in GreenView.xib". Well, yea, from your crash, it is. But are you sure its connected to the switchViews: function of a GreenViewController instance? How did you check this information?

    What I would suggest is deleting all connections to the UIButton in the connection inspector. Then reconnecting it to the view controller (which you say is a GreenViewController). It should then bring up the list of IBActions which should only be the switchViews: method.

    If you do that, and it still doesn't work. Try deleting the button and remaking it, then reconnecting it.