ios Objective-C 传递方法作为参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7936472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective-C passing methods as parameters
提问by Hymansonkr
How do you pass one method as a parameter to another method? I'm doing this across classes.
如何将一个方法作为参数传递给另一个方法?我正在跨班级这样做。
Class A:
A类:
+ (void)theBigFunction:(?)func{
// run the func here
}
Class B:
B类:
- (void)littleBFunction {
NSLog(@"classB little function");
}
// somewhere else in the class
[ClassA theBigFunction:littleBFunction]
Class C:
C类:
- (void)littleCFunction {
NSLog(@"classC little function");
}
// somewhere else in the class
[ClassA theBigFunction:littleCFunction]
回答by Filip Radelic
The type you are looking for is selector (SEL
) and you get a method's selector like this:
您正在寻找的类型是选择器 ( SEL
) 并且您会得到一个方法的选择器,如下所示:
SEL littleSelector = @selector(littleMethod);
If the method takes parameters, you just put :
where they go, like this:
如果该方法接受参数,您只需将:
它们放在何处,如下所示:
SEL littleSelector = @selector(littleMethodWithSomething:andSomethingElse:);
Also, methods are not really functions, they are used to send messages to specific class (when starting with +) or specific instance of it (when starting with -). Functions are C-type that doesn't really have a "target" like methods do.
此外,方法并不是真正的函数,它们用于向特定类(以 + 开头时)或它的特定实例(以 - 开头时)发送消息。函数是 C 类型的,它不像方法那样真正具有“目标”。
Once you get a selector, you call that method on your target (be it class or instance) like this:
一旦你得到一个选择器,你就可以像这样在目标(无论是类还是实例)上调用该方法:
[target performSelector:someSelector];
A good example of this is UIControl
's addTarget:action:forControlEvents:method you usually use when creating UIButton
or some other control objects programmatically.
这方面的一个很好的例子是UIControl
的addTarget:动作:forControlEvents:在创建时通常使用的方法UIButton
或一些其他的控制对象编程。
回答by bryanmac
Another option is to look at blocks. It allows you to pass a block of code (a closure) around.
另一种选择是查看块。它允许你传递一个代码块(一个闭包)。
Here's a good write up on blocks:
这是一个关于块的好文章:
http://pragmaticstudio.com/blog/2010/7/28/ios4-blocks-1
http://pragmaticstudio.com/blog/2010/7/28/ios4-blocks-1
Here's the apple docs:
这是苹果文档:
回答by MJD
Objective C makes this operation relatively easy. Apple provides this documentation.
目标 C 使这个操作相对容易。Apple 提供了此文档。
To directly address your question, you are not calling a function, but a selector. Here is some sample code:
要直接解决您的问题,您不是在调用函数,而是在调用选择器。下面是一些示例代码:
Big Function:
大功能:
+ (void)theBigFunction:(SEL)func fromObject:(id) object{
[object preformSelector:func]
}
Then for class B:
然后对于B类:
- (void)littleBFunction {
NSLog(@"classB little function");
}
// somewhere else in the class
[ClassA theBigFunction:@selector(littleBFunction) fromObject:self]
Then for class C:
然后对于 C 类:
- (void)littleCFunction {
NSLog(@"classC little function");
}
// somewhere else in the class
[ClassA theBigFunction:@selector(littleCFunction) fromObject:self]
EDIT: Fix selectors sent (remove the semicolon)
编辑:修复发送的选择器(删除分号)