![](/img/trans.png)
[英]Objective-C Base Class Property Custom Getter Not Being Called From Subclass
[英]Objective-c get base class property from subclass
我创建了 in.h 文件 @property(例如 BOOL 值)。 在.m 文件中,我更改了 BOOL 值。
在子类代码中,我想获取此属性NSLog(@"%@", _value)
的值,但值始终为 null。
基本 class 代码:
.h 文件:
@interface CommonViewController : UIViewController <MPPGraphDelegate, MPPInputSourceDelegate>
...
@property(nonatomic) BOOL buttonState;
@property(nonatomic) UIButton* button;
...
@end
.mm 文件:
- (void)viewDidLoad {
[super viewDidLoad];
...
_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[_button setTitle:@"Show View" forState:UIControlStateNormal];
_button.frame = CGRectMake(100, 500, 120, 30);
[self.view addSubview:_button];
}
-(void)aMethod:(UIButton*) sender {
if (sender.isSelected) {
sender.selected = NO;
_buttonState = NO; <-- change value
} else {
sender.selected = YES;
_buttonState = YES; <-- change value
}
}
子类代码
.h 文件:
@interface HandTrackingViewController : CommonViewController
...
@end
.mm 文件:
@implementation HandTrackingViewController
@synthesize buttonState = _buttonState;
- (void)someMethod {
NSLog(_buttonState ? @"YES" : @"NO"); <-- _buttonState always null
}
@end
没有@synthesize
的self.buttonState
解决了我的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.