0

手动构建:

[btnRun addTarget:self action:@selector(RunApp:) forControlEvents:UIControlEventTouchUpOutside];

以编程方式构建:类似于以下内容??

- (void) setRunButton:(UIButton*)objectName mySelector:(NSString*)funcName myControlEvent:(NSString*) controlEvent
{
    [objectName addTarget:self action:@selector(funcName) forControlEvents:controlEvent];
}
4

2 回答 2

3

我认为您需要以下内容:

- (void)setRunButton:(UIButton *)objectName mySelector:(NSString *)action myControlEvent:(UIControlEvents)controlEvent {

   [objectName addTarget:self action:NSSelectorFromString(action) forControlEvents:controlEvent];

}

将选择器作为 an 传递是不常见的,NSString但您可以使用NSSelectorFromString()将选择器的字符串名称转换为选择器。

控制事件参数不是字符串,它们是枚举,因此我已将myControlEvent参数更改为具有UIControlEvents类型。

将选择器传递给使用@selector(action). 但是,@selector在编译时处理,因此参数实际上不是NSString. 在这种情况下,该方法如下所示:

- (void)setRunButton:(UIButton *)objectName mySelector:(SEL)action myControlEvent:(UIControlEvents)controlEvent {

   [objectName addTarget:self action:action forControlEvents:controlEvent];

}
于 2012-06-24T04:10:00.060 回答
1

将整个选择器作为参数传递

- (void) setRunButton:(UIButton*)objectName mySelector:(SEL)action myControlEvent:(NSString*) controlEvent
{
    [objectName addTarget:self action:action forControlEvents:controlEvent];
}
于 2012-06-24T04:07:27.780 回答