0

我正在编写一个自定义帮助方法,该方法会被大量使用并返回几个按钮。每个按钮在按下时当然会有自己的目标选择器,我想将选择器作为参数传递给此方法,以便返回的按钮具有指定的选择器。

但我不确定如何将选择器作为方法参数传递。像这样的东西:

-(returnedInstance)someMethod:(WhatClass?*)selectedFunction{

[SomeClassWithASelectorParameter method:whatever selector:@selector(selectedFunction)];

}

其中selectedFunction是传递给方法的参数。

我尝试制作WhatClass?*NSString 和 SEL,但结果是:

[NSInvocation invocationWithMethodSignature:]:方法签名参数不能为nil

4

2 回答 2

4

你为什么不只是通过一个SEL?即选择器。像这样:

-(returnedInstance)someMethod:(SEL)selectedFunction{
    [SomeClassWithASelectorParameter method:whatever selector:selectedFunction];
}

或者,NSSelectorFromString

-(returnedInstance)someMethod:(NSString*)selectedFunction{
    [SomeClassWithASelectorParameter method:whatever selector:NSSelectorFromString(selectedFunction)];
}
于 2012-02-23T09:04:39.773 回答
1

您想使用SEL,当您引用它时,您不必使用selector

-(returnedInstance)someMethod:(SEL)selectedFunction{

    [SomeClassWithASelectorParameter method:whatever selector:selectedFunction];

}
于 2012-02-23T09:05:25.770 回答