2

单击菜单时将调用以下块。

CCMenuItem* iconItem = [CCMenuItemSprite itemFromNormalSprite:icon 
                                          selectedSprite:icon2 
                                          block:^(id sender) {
    [self buttonClickedWithUnlocked:unlocked world:world];
}];

我可以将参数传递给块内的方法。

但现在我正在将 obj-c 翻译成 c++。因此不支持块。我需要使用像这样的选择器:

 CCMenuItem* iconItem = 
 CCMenuItemSprite::itemFromNormalSprite(icon, icon2, this,              
 menu_selector(SomeClass::buttonClickedWithUnlockedWorld(unlocked, w)));

但我无法传递参数(选择器不支持)所以还有其他方法吗?

如果您不使用 c++,则选择器的 Obj-c 版本是:

CCMenuItem* iconItem = [CCMenuItemSprite itemFromNormalSprite:icon 
                                          selectedSprite:icon2 
                                          target: self
                                          selector: @selector(buttonClickedWithUnlocked)
}];

但是,我也不能将任何参数传递给@selector。还有其他方法吗?

4

1 回答 1

2

Your obj-c version should be selector: @selector(buttonClickedWithUnlocked::) instead of selector: @selector(buttonClickedWithUnlocked) (Note the ::). So for your C++ version you need to create something like [target performSelector:selector withObject:obj1 withObject:obj2].

So in Obj-C it could look like this:

CCMenuItem* iconItem = [CCMenuItemSprite itemFromNormalSprite:icon 
                                      selectedSprite:icon2 
                                      target: self
                                      selector: @selector(buttonClickedWithUnlocked::)
                                      param1: [NSNumber numberWithInt:unlocked]
                                      param2: [NSNumber numberWithBool:w]
}];

and buttonClickedWithUnlocked must be defined to have two NSNumber parameters.

于 2012-05-10T11:31:56.817 回答