I set up my user interface in an xib file and got everything working pretty well. But the xib file was pretty cluttered, so I decided to try to create some of the buttons/bars/etc. programmatically instead. I can get them to show up fine, but I’m doing something wrong with the selectors.
With an instructions button on the xib file, for instance, I just used the mouse to connect the instructions button with the giveInstructions method in the .h file, and everything was fine. But when I try it programmatically, like so:
UIBarButtonItem *instr = [[UIBarButtonItem alloc] initWithTitle:@"Instructions"
style:UIBarButtonSystemItemDone target:nil action:@selector(giveInstructions:)];
I get an unrecognized selector sent to instance error. What am I doing wrong?
Seeing as you fixed the target problem, it is most likely that “giveInstructions” doesn’t exist, at least not in this class’s implementation.
If you have declared
In your .h, it is imperative that you have a matching implementation in your .m file, e.g.:
If you have done this already, then the only other thing I can think of that could be wrong is that in your
@selector(giveInstructions:)the colon may be unnecessary if you don’t have a sender, or any other arguments passed from the method.Note: When calling
@selectorif the method you are calling is 1 character different from its actual declared name you will get this error because the selector doesn’t exist.