How to pass method arguments to a selector

时间:2022-11-20 23:18:42

If I have a method like this:

如果我有这样的方法:

- (void) foo
{
}

Then I can access it through a selector like this:

然后我可以通过这样的选择器访问它:

@selector(foo)

But what if I have a method like this:

但是,如果我有这样的方法怎么办:

- (void) bar:(NSString *)str arg2:(NSString *)str2
{

}

Then how do I access it through a selector?

那我该如何通过选择器访问它?

2 个解决方案

#1


5  

To handle an arbitrary number of selectors you should use NSInvocation, but you can handle up to two objects using the standard performWithSelector stuff

要处理任意数量的选择器,您应该使用NSInvocation,但是您可以使用标准的performWithSelector东西处理最多两个对象

[foo performSelector:@selector(bar:arg2:) withObject:obj1 withObject:obj2]

[foo performSelector:@selector(bar:arg2 :) withObject:obj1 withObject:obj2]

#2


2  

Remove the spaces, parameter types, and parameter names. In your example, this would become:

删除空格,参数类型和参数名称。在您的示例中,这将变为:

@selector(bar:arg2:)

#1


5  

To handle an arbitrary number of selectors you should use NSInvocation, but you can handle up to two objects using the standard performWithSelector stuff

要处理任意数量的选择器,您应该使用NSInvocation,但是您可以使用标准的performWithSelector东西处理最多两个对象

[foo performSelector:@selector(bar:arg2:) withObject:obj1 withObject:obj2]

[foo performSelector:@selector(bar:arg2 :) withObject:obj1 withObject:obj2]

#2


2  

Remove the spaces, parameter types, and parameter names. In your example, this would become:

删除空格,参数类型和参数名称。在您的示例中,这将变为:

@selector(bar:arg2:)