手动构建:
[btnRun addTarget:self action:@selector(RunApp:) forControlEvents:UIControlEventTouchUpOutside];
程序化构建:
Programmatically built: something of the following like ??
- (void) setRunButton:(UIButton*)objectName mySelector:(NSString*)funcName myControlEvent:(NSString*) controlEvent { [objectName addTarget:self action:@selector(funcName) forControlEvents:controlEvent]; }推荐答案
像下面这样:
- (void)setRunButton:(UIButton *)objectName mySelector:(NSString *)action myControlEvent:(UIControlEvents)controlEvent { [objectName addTarget:self action:NSSelectorFromString(action) forControlEvents:controlEvent]; }将选择器作为 NSString ,但您可以使用 NSSelectorFromString()将选择器的字符串名称转换为选择器。
It is unusual to pass a selector as an NSString but you can use NSSelectorFromString() to convert the string name of the selector into a selector.
控制事件参数不是它们是枚举的字符串,所以我改变了 myControlEvent 参数, c $ c> UIControlEvents 类型。
Control events parameters are not strings they are an enumeration so I have changed the myControlEvent parameter to have the UIControlEvents type.
这将是更通常的选择器传递到方法使用 @selector (action)。但是, @selector 在编译时处理,因此参数实际上不是 NSString 。在这种情况下,方法看起来像:
It would be more usual to pass the selector to the method using @selector(action). However, @selector is handled at compile time so the parameter isn't actually an NSString. In this case the method would look like:
- (void)setRunButton:(UIButton *)objectName mySelector:(SEL)action myControlEvent:(UIControlEvents)controlEvent { [objectName addTarget:self action:action forControlEvents:controlEvent]; }更多推荐
如何以编程方式为UIButton设置CallBack?
发布评论