2

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?

3
  • You need to set the target. It will most likely be self as in when the button is pressed call the method giveInstructions: on this object
    – Paul.s
    Aug 11, 2012 at 20:37
  • The error message explains that the instance (target, in your situation 'nil' or non-existent) doesn't recognise the selector giveInstructions:. Aug 11, 2012 at 20:40
  • 1
    why everybody suggests that the crash is due to setting the target to nil? Messaging nil should be fine in Obj-C...
    – user529758
    Aug 11, 2012 at 20:54

4 Answers 4

2

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

- (void)giveInstructions:(id)sender;

In your .h, it is imperative that you have a matching implementation in your .m file, e.g.:

- (void)giveInstructions:(id)sender{
    //my code here
}

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 @selector if 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.

1
  • Ah, that's what did it! giveInstructions didn't have any arguments. So when I changed the selector to @selector(giveInstructions), without the :, it worked fine. Thanks so much for the help. Aug 11, 2012 at 20:54
2

Your target should not be nil but rather the instance which should receive the action. That's probably self in your case, so try this:

UIBarButtonItem *instr = [[UIBarButtonItem alloc] initWithTitle:@"Instructions" style:UIBarButtonSystemItemDone target:self action:@selector(giveInstructions:)];
2
  • What does your method definition look like? Should be - (void)giveInstructions:(id)sender;
    – Sascha
    Aug 11, 2012 at 20:44
  • Yup, that was it--the method definition didn't take any arguments. So when I took the colon out of the selector, everything worked fine. Thanks! Aug 11, 2012 at 20:55
2

The naming of the selector is important.

If it has one colon then it refers to a method that takes one argument

@selector(myMethod:)

- (void)myMethod:(id)sender;

if it does not have a colon then the method does not expect an argument

@selector(myMethod)

- (void)myMethod;

Note Although they both look like they have the same name they are in fact two different methods.

1
UIBarButtonItem *instr = 
[[UIBarButtonItem alloc] initWithTitle:@"Instructions" 
style:UIBarButtonSystemItemDone target:self action:@selector(giveInstructions:)];
<-- change "nil" to "self" !

I think the target:nil should be self because those functions are belongs to this class file.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.