Question about Objective-C selectors; Can I use parameters and how?

丶灬走出姿态 提交于 2019-12-13 01:54:17

问题


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?

Thanks in advance:D


回答1:


You can't. Selectors specify only method to be invoked, not parameters to be passed.

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.

To tell different ui elements apart, tag attribute is often used. So, code could look like

if ([sender tag] == 1) {
    ...
} else if ...

If you don't like too many ifs, lookup table will work.




回答2:


Are you maybe looking for performSelector:withObject? I'm afraid I don't quite understand your questions maybe.

Nikita is right, when you are setting up the selector you just pass in the descriptor name. Later in your code, when you call the method, you will pass in any arguments.



来源:https://stackoverflow.com/questions/4981232/question-about-objective-c-selectors-can-i-use-parameters-and-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!