通过KVC为UIButton分配辅助值

时间:2015-02-13 19:14:32

标签: uibutton kvc

如何为UIButton分配辅助信息?
是否可以通过KVC技术分配:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSNumber *dateNumb = [NSNumber numberWithInteger:15];
    [self.myButton setValue:dateNumb forKey:@"day"];
}

我收到以下错误:

  

***由于未捕获的异常终止应用程序' NSUnknownKeyException',原因:' ... setValue:forUndefinedKey:]:此类不是键值   符合编码标准的关键日。'

1 个答案:

答案 0 :(得分:0)

我可以通过UIButton的图层这样做;不是UIButton本身:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSNumber *dateNumb = [NSNumber numberWithInteger:15];

    CALayer *myLayer = self.myButton.layer;
    [myLayer setValue:dateNumb forKey:@"day"];
      //  [self.myButton setValue:dateNumb forKey:@"day"];
}

(lldb) po sender.layer
<CALayer:0x7fb5f2819800; position = CGPoint (188 241); bounds = CGRect (0 0; 46 30); delegate = <UIButton: 0x7fb5f281abe0; frame = (165 226; 46 30); opaque = NO; 
autoresize = RM+BM; layer = <CALayer: 0x7fb5f2819800>>; 
sublayers = (<_UILabelLayer: 0x7fb5f178eba0>); 
allowsGroupOpacity = YES; day = 15>