在具有关联引用的UIView类别中使用子类UILabel

时间:2013-05-17 17:05:00

标签: ios objective-c subclass objective-c-category

我正在尝试在UIView类别中使用MTAnimatedLabel类(https://github.com/mturner1721/MTAnimatedLabel)(来自这里的scalessec的iOS Toast:https://github.com/scalessec/Toast

我知道我可能需要使用Obj-C相关引用。以下是我试图这样做的方法:

    MTAnimatedLabel *existingTestLabel = (MTAnimatedLabel *)objc_getAssociatedObject(self, &testLabelKey);
if(existingTestLabel != nil) return;

MTAnimatedLabel *testLabel = [[[MTAnimatedLabel alloc] initWithFrame:testFrame] autorelease];
testLabel.text = [NSString stringWithFormat:@"Test String."];
NSLog(@"testLabel: %@", testLabel.text);
objc_setAssociatedObject(self, &testLabelKey, testLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self addSubview:testLabel];
[testLabel startAnimation];

它在此行上以EXC_BAD_ACCESS失败:

    MTAnimatedLabel *testLabel = [[[MTAnimatedLabel alloc] initWithFrame:testFrame] autorelease];

我的testLabelKey定义为:

static char const * const testLabelKey = "testLabel";

我的testLabel propety在接口中声明为:

@property (nonatomic, retain) MTAnimatedLabel *testLabel;

任何人都可以发现我做错了什么,或者建议采用不同的方式吗?谢谢!

0 个答案:

没有答案