如何为具有多个参数的方法创建 CKComponentViewAttribute
How to create a CKComponentViewAttribute for a method with multiple arguments
我想为 UIButton 子类创建一个组件。要设置按钮,我想设置图像。我的问题是我不知道如何制作一个 CKComponentViewAttribute,它接受 UIButton 方法 setImage:forState:
.
所需的两个参数
我试过这个:
[CKComponent newWithView:{
[KGHitTestingButton class],
{
{CKComponentActionAttribute(@selector(onMenuTap))},
{@selector(setImage:forState:), [UIImage imageNamed:@"location_action"]}, @(UIControlStateNormal)},
}
} size:{.width = 30, .height = 30}]
但是那不会编译。与此版本相同:
{@selector(setImage:forState:), @[[UIImage imageNamed:@"location_action"]}, @(UIControlStateNormal)]},
}
我在这里 http://componentkit.org/docs/advanced-views.html 读到我需要将我的输入框到一个对象中。在这种情况下我该怎么做?
更新:
我在这里找到了以下提示 https://github.com/facebook/componentkit/issues/265 :
"CKComponentViewAttribute 可以用任意块初始化。下面是一个例子:
static const CKComponentViewAttribute titleShadowColorAttribute = {"MyComponent.titleShadowColor", ^(UIButton *button, id value){
[button setTitleShadowColor:value forState:UIControlStateNormal];
}};
这就是我最终设法解决问题的方法:
static const CKComponentViewAttribute imageAttribute = {"LocationActionButton.imageAttribute", ^(UIButton *button, id value){
[button setImage:value forState:UIControlStateNormal];
}};
CKComponent *locationActionButton = [CKComponent newWithView:{
[UIButton class],
{
{@selector(setBackgroundColor:), [UIColor redColor]},
{imageAttribute, [UIImage imageNamed:@"location_action"]}
}
} size:{.width = 30, .height = 30}];
您可以内联以使其更短:
CKComponent *locationActionButton = [CKComponent newWithView:{
[UIButton class],
{
{@selector(setBackgroundColor:), [UIColor redColor]},
{{"LocationActionButton.imageAttribute", ^(UIButton *button, id value){
[button setImage:value forState:UIControlStateNormal];
}}, [UIImage imageNamed:@"location_action"]}
}
} size:{.width = 30, .height = 30}];
解决此问题的一种方法是为 class 创建一个私有类别,该类别传递带有参数的集合,然后按预期应用。所以像:
@interface KGHitTestingButton (CKPrivate)
- (void)_ckSetImageforState:(NSArray*)args;
@end
@implementation
- (void)_ckSetImageforState:(NSArray*)args {
[self setImage:args[0] forState:[args[1] integerValue]];
}
@end
...
[CKComponent newWithView:{
[KGHitTestingButton class],
{
{@selector(_ckSetImageforState:), @[[UIImage imageNamed:@"location_action"]}, @(UIControlStateNormal)]},
}
...
它不是很漂亮,但它可以绕过这个限制。 NSDictionary
是替代方案,但需要定义一些静态键。
CKComponentViewAttribute 有 2 个初始值设定项。一种是使用选择器,这是我们最常用的方法。
CKComponentViewAttribute(SEL setter);
// e.g. ' { @selector(setBackgroundColor:), color} '
另一个是
CKComponentViewAttribute(const std::string &ident,
void (^app)(id view, id value),
void (^unapp)(id view, id value) = nil,
void (^upd)(id view, id oldValue, id newValue) = nil) ;
它需要一个标识符和 3 个块来配置不同条件下的视图。参考更多表格CKComponentViewAttribute.h
,例如"CKComponentGestureActions.h"
。
如您的情况:
{ {"setImage",
^(UIButton *button, id value) {
[button setImage:theImageObject forState:UIControlStateNormal]
},
@0 // Bogus value, we don't use it.
}
我想为 UIButton 子类创建一个组件。要设置按钮,我想设置图像。我的问题是我不知道如何制作一个 CKComponentViewAttribute,它接受 UIButton 方法 setImage:forState:
.
我试过这个:
[CKComponent newWithView:{
[KGHitTestingButton class],
{
{CKComponentActionAttribute(@selector(onMenuTap))},
{@selector(setImage:forState:), [UIImage imageNamed:@"location_action"]}, @(UIControlStateNormal)},
}
} size:{.width = 30, .height = 30}]
但是那不会编译。与此版本相同:
{@selector(setImage:forState:), @[[UIImage imageNamed:@"location_action"]}, @(UIControlStateNormal)]},
}
我在这里 http://componentkit.org/docs/advanced-views.html 读到我需要将我的输入框到一个对象中。在这种情况下我该怎么做?
更新: 我在这里找到了以下提示 https://github.com/facebook/componentkit/issues/265 :
"CKComponentViewAttribute 可以用任意块初始化。下面是一个例子:
static const CKComponentViewAttribute titleShadowColorAttribute = {"MyComponent.titleShadowColor", ^(UIButton *button, id value){
[button setTitleShadowColor:value forState:UIControlStateNormal];
}};
这就是我最终设法解决问题的方法:
static const CKComponentViewAttribute imageAttribute = {"LocationActionButton.imageAttribute", ^(UIButton *button, id value){
[button setImage:value forState:UIControlStateNormal];
}};
CKComponent *locationActionButton = [CKComponent newWithView:{
[UIButton class],
{
{@selector(setBackgroundColor:), [UIColor redColor]},
{imageAttribute, [UIImage imageNamed:@"location_action"]}
}
} size:{.width = 30, .height = 30}];
您可以内联以使其更短:
CKComponent *locationActionButton = [CKComponent newWithView:{
[UIButton class],
{
{@selector(setBackgroundColor:), [UIColor redColor]},
{{"LocationActionButton.imageAttribute", ^(UIButton *button, id value){
[button setImage:value forState:UIControlStateNormal];
}}, [UIImage imageNamed:@"location_action"]}
}
} size:{.width = 30, .height = 30}];
解决此问题的一种方法是为 class 创建一个私有类别,该类别传递带有参数的集合,然后按预期应用。所以像:
@interface KGHitTestingButton (CKPrivate)
- (void)_ckSetImageforState:(NSArray*)args;
@end
@implementation
- (void)_ckSetImageforState:(NSArray*)args {
[self setImage:args[0] forState:[args[1] integerValue]];
}
@end
...
[CKComponent newWithView:{
[KGHitTestingButton class],
{
{@selector(_ckSetImageforState:), @[[UIImage imageNamed:@"location_action"]}, @(UIControlStateNormal)]},
}
...
它不是很漂亮,但它可以绕过这个限制。 NSDictionary
是替代方案,但需要定义一些静态键。
CKComponentViewAttribute 有 2 个初始值设定项。一种是使用选择器,这是我们最常用的方法。
CKComponentViewAttribute(SEL setter);
// e.g. ' { @selector(setBackgroundColor:), color} '
另一个是
CKComponentViewAttribute(const std::string &ident,
void (^app)(id view, id value),
void (^unapp)(id view, id value) = nil,
void (^upd)(id view, id oldValue, id newValue) = nil) ;
它需要一个标识符和 3 个块来配置不同条件下的视图。参考更多表格CKComponentViewAttribute.h
,例如"CKComponentGestureActions.h"
。
如您的情况:
{ {"setImage",
^(UIButton *button, id value) {
[button setImage:theImageObject forState:UIControlStateNormal]
},
@0 // Bogus value, we don't use it.
}