Reputation: 18149
I'm using a Objective-C framework for game development called Cocos2d-iphone.
This is how I create a button-graphic in the game:
CCMenuItemImage *battle;
battle = [CCMenuItemImage itemFromNormalImage:@"BattleFightOption1.png" selectedImage:@"BattleFightOption2.png"
target:self selector:@selector(battleFightOption)];
Basically, when the user clicks the button, method battleFightOption
runs.
But I wonder, I never did define battleFightOption
in the interface.. so, my question is: when is it necessary to define a method in the interface, and when is it not?
Upvotes: 1
Views: 84
Reputation: 1089
When you use a selector like @selector(methodName:)
, methodName:
is called dynamically at runtime. The compiler doesn't have to know where it is, and doesn't check that the method exists when you compile.
However, it is still a good idea to declare it privately, which is generally done by putting an unnamed category at the top of the .m
file (generally referred to as a class extension):
#import "Class.h"
@interface Class ()
- (void)privateMethod;
@end
@implementation Class
...
Upvotes: 1
Reputation: 57179
Anything that you intend to be public, called outside of the class, should be defined in the interface. If you are going to only use @selector(battleFightOption)
you really do not need to define the method anywhere but I would recommend that you add a definition in the class extension just as you would any other private method.
Upvotes: 0
Reputation: 28572
In short, every method that is meant to be used from outside the class must be declared in the interface; methods that are internal to the class implementation are omitted. The latter are typically declared in a class extension.
Upvotes: 1