0

what I want to do is I created my own button class but I need to provide the ability for the developer to have their own button callback.

For example, I can declare a new button in this way:

Button* myButton = [[Button alloc] init];
// What I want is something like this
[myButton setSelector: @selector(callMe)];

// and I have this method implemented
- (void)callMe
{
    NSLog("I'm being called");
}

Inside my button class I need to have one variable to store what function it will be calling. For example, in my Button class:

if (onButtonClick)
   [self callSelector];

How can I do that?

EDIT: I've found a solution here: How to perform Callbacks in Objective-C

Community
  • 1
  • 1
Cadrick Loh
  • 721
  • 1
  • 7
  • 19

1 Answers1

0

Simple:

if (onButtonClick)
   [self performSelector:callSelector];

Good time to read up on the base class of all Objective-C classes (NSObject) because you'll need stuff like that quite regularly.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • I know about this, but I want to know is there any way I can store which function to be called? I did think of using delegate but I found out that's not really suit in my case... Basically what i want is something like CCMenuItem.. when I init I can pass in the @selector(someFunc:)... I dig in the codes and notice they are using block to do it, I was thinking is there any other ways? – Cadrick Loh Oct 23 '12 at 03:02