0

我想做的是创建自己的按钮类,但我需要为开发人员提供拥有自己的按钮回调的能力。

例如,我可以这样声明一个新按钮:

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");
}

在我的按钮类中,我需要一个变量来存储它将调用的函数。例如,在我的 Button 类中:

if (onButtonClick)
   [self callSelector];

我怎样才能做到这一点?

编辑:我在这里找到了一个解决方案: How to perform Callbacks in Objective-C

4

1 回答 1

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.

于 2012-10-22T17:56:30.383 回答