NSInvalidArgumentException : unrecognized selector

Discussion in 'iOS Programming' started by fergusjk, Sep 5, 2010.

  1. fergusjk macrumors member

    Joined:
    Aug 23, 2010
    Location:
    Ayrshire Coast, Scotland
    #1
    Here is the situation.

    I am using a tabbar application. I have 2 tabs (views) each with their own view controllers.
    FirstViewController (.m and .h)
    SecondViewController (.m and .h)

    I have changed files owner class identity to FirstViewController for FirstView.xib and I have changed files owner class identity to SecondViewController for SecondView.xib

    Both views have a UIButton.

    When i click the button on the opening (FirstView) pane it runs the corresponding action code it is hooked up to.
    -(IBAction) button1Pressed{
    NSLog(@"in button1Pressed");
    }

    I have similar code for the second button in the other pane.
    -(IBAction) button2Pressed{
    NSLog(@"in button2Pressed");
    }
    But when I click on the button on the second pane the application crashes with the following error
    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController button2Pressed]: unrecognized selector sent to instance 0x59243b0'

    I thought the UIViewController may be the class it is trying to find the method button2Pressed? I would find this odd though as I stated I have changed the files owner class identity to SecondViewController for SecondView.xib.

    Sorry if some of the above seems a bit garbled, I have quite a bit of programming experience but I'm a noob at iPhone/objective c.
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    The button is not correctly connected to its target. Either the target is wrong or the action is wrong.

    You could log self from the view controller that should be the target in its viewDidLoad method and then compare it to the instance that's printed in the error message. A common error is that the action selector is wrong in some way. Perhaps you changed the spelling of the name of the action after making the connections in IB.

    Inspect the actions in IB in the HUD window for file's owner and for the button.
     
  3. fergusjk thread starter macrumors member

    Joined:
    Aug 23, 2010
    Location:
    Ayrshire Coast, Scotland
    #3
    Hi,

    The tabbar app is very simple and I done the same few simple steps for both the FirstView and the SecondView. I've done it a few times now as I've been wrestling with this for several hours now.

    I included the following in each controller
    FirstViewController.m
    - (void)viewDidLoad {
    [self button1Pressed];
    [super viewDidLoad];
    }

    SecondViewController.m
    - (void)viewDidLoad {
    [self button2Pressed];
    [super viewDidLoad];
    }

    When the app loads up I get in "button1Pressed" - which is good.
    But when I click on the second tab - the view is visible but there is no "in button2Pressed". I can see and even press the button (and causing the resultant crash) created using IB.

    It is almost as though the second view never loads, but it clearly does because I can use the UIElements?
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    The error message is targeted to a UIViewController, not your custom view controller so probably the second view controller isn't being created. Are you sure that in your MainWindow.xib the type is correct?
     
  5. fergusjk thread starter macrumors member

    Joined:
    Aug 23, 2010
    Location:
    Ayrshire Coast, Scotland
    #5
    SOLVED!!!

    I didn't change the class identity for SecondView in MainWindow.xib.

    I changed the class identities in FirstView.xib and SecondView.xib but this, it would appear, is not sufficient.

    It seems that in MainWindow.xib the identity for FirstView is automatically set to FirstViewController but any subsequent views inc. SecondView have to be manually set. This behaviour is reasonable I suppose given you are going to create your own controllers and IB wouldn't know what they were going to be called.

    Thanks Phoney, this was much appreciated :)
     

Share This Page