好了,所以我有這個,但它不會工作:設置類別界面新特性/實現
@interface UILabel (touches)
@property (nonatomic) BOOL isMethodStep;
@end
@implementation UILabel (touches)
-(BOOL)isMethodStep {
return self.isMethodStep;
}
-(void)setIsMethodStep:(BOOL)boolean {
self.isMethodStep = boolean;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if(self.isMethodStep){
// set all labels to normal font:
UIFont *toSet = (self.font == [UIFont fontWithName:@"Helvetica" size:16]) ? [UIFont fontWithName:@"Helvetica-Bold" size:16] : [UIFont fontWithName:@"Helvetica" size:16];
id superView = self.superview;
for(id theView in [(UIView *)superView subviews])
if([theView isKindOfClass:[UILabel class]])
[(UILabel *)theView setFont:[UIFont fontWithName:@"Helvetica" size:16]];
self.font = toSet;
}
}
@end
如果我參加了getter和setter方法,然後它不工作,它告訴我,我需要創建一些getter和setter方法(或使用@synthesize - 但將@synthesize放在@implementation中也會引發錯誤)。但是通過getter和setter方法,我得到了一個EXC_BAD_ACCESS和一個崩潰。有任何想法嗎?由於
湯姆
這傢伙'Dave DeLong'發佈了一個答案,你應該檢查出...這可能是「正確」的方式來做你想做的...... :) [這裏是鏈接...](http ://stackoverflow.com/questions/4146183/instance-variables-for-objective-c-categories) – 2011-07-04 15:54:10
關聯引用解決方案...在這裏http://www.techpaa.com/2012/04/adding找到好的教程-properties-to-categories -.html – ShivaPrasad 2012-04-17 11:47:35
另外,你的'isMethodStep' getter是一個無限遞歸函數。這就是爲什麼你會遇到EXC_BAD_ACCESS崩潰。 'self.isMethodStep'等同於'[self isMethodStep]'。 – 2014-10-06 18:43:33