关于Objective-C选择器的问题;我可以使用参数和如何? [英] Question about Objective-C selectors; Can I use parameters and how?
问题描述
我尝试使用带有参数的选择器,并且在这样做时失败。我来自C / ++和选择器是一个有点混乱。我有这个代码:
I'm attempting to use a selector with arguments and failing while doing so. I'm coming from C/++ and selectors are a tad bit confusing. I have this code:
playItem = [CCMenuItemLabel itemWithLabel:playLabel target:self selector:@selector(goToScene:)argumentHere];
如何以这种方式将参数传递给方法?
How would I go about passing an argument to a method in this way?
提前感谢:D
推荐答案
选择器仅指定要调用的方法,而不指定要传递的参数。
You can't. Selectors specify only method to be invoked, not parameters to be passed.
可以执行的操作是检查 sender
参数在 goToScene:
方法中。它将是执行操作的元素(很可能是 CCMenuItemLabel
)。
因此,您可以看到调用了哪个元素你使用 goToScene:
几个ui元素),并决定使用哪个'参数'。
What you can do, is to check sender
parameter in your goToScene:
method. It's gonna be the element on which action is performed (most probably CCMenuItemLabel
in your case).
Thus, you can see which element was invoked (if you use goToScene:
for several ui elements) and decide which 'parameter' to use.
为了区分不同的ui元素,通常使用标记
属性。所以,代码看起来像
To tell different ui elements apart, tag
attribute is often used. So, code could look like
if ([sender tag] == 1) {
...
} else if ...
如果你不喜欢太多的ifs,工作。
If you don't like too many ifs, lookup table will work.
这篇关于关于Objective-C选择器的问题;我可以使用参数和如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!