2

In my iPhone application I have such code:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Instruction Controller";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(menuButtonClicked)];   
}   


#pragma mark private methodes

-(void) menuButtonClicked{
    NSLog(@"menuButtonClicked");
}

But when I click on this button it raise an exeption: "Unrecognized selector sent to instance". How cam I resolve this issue?

UPDATE

2013-05-23 12:21:33.182 CICDP[3091:11603] -[__NSCFString menuButtonClicked]: unrecognized selector sent to instance 0x8128eb0 2013-05-23 12:21:33.188 CICDP[3091:11603] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString menuButtonClicked]: unrecognized selector sent to instance 0x8128eb0'

revolutionkpi
  • 2,632
  • 10
  • 45
  • 84

3 Answers3

5

It looks like your controller class (the target of the button action) is being released before the button fires. The button is likely retained by its superview.

You need to do something to keep the controller class alive (some other instance needs to hold a strong reference to it) for the whole time the button is on display.

Wain
  • 118,658
  • 15
  • 128
  • 151
0

I have got the same issue but it wasn't a reference issue. The log 'Unknown class MyViewController in Interface Builder file.' helps me a lot.

I have solved this issue with the answer : Xcode 6 Strange Bug: Unknown class in Interface Builder file

To resolve this issue, be sure the module associated to your view is correctly set :

If you see Module None, there is the issue

enter image description here

Go on Module and just tap enter to see this :

enter image description here

Community
  • 1
  • 1
Kevin ABRIOUX
  • 16,507
  • 12
  • 93
  • 99
-3

You need to add a : to the selector and change the method

- (void)viewDidLoad

{

    [super viewDidLoad];

    self.title = @"Instruction Controller";

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] 
        initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self 
        action:@selector(menuButtonClicked:)];   

}

-(void) menuButtonClicked:(id)sender {
    NSLog(@"menuButtonClicked");
}
  • the : in the selector is only optional. if you add one, the element returns reference for it self, but it also works fine if you use a selector without (id)sender ;) – geo May 23 '13 at 09:32