objective-c - Objective-c 从子类获取基类属性
问题描述
我在 .h 文件中创建了 @property(例如 BOOL 值)。在 .m 文件中,我更改了 BOOL 值。
在子类代码中,我想获取此属性的NSLog(@"%@", _value)
值,但值始终为空。
基类代码:
.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
解决方案
self.buttonState
没有@synthesize
解决我的问题
推荐阅读
- python - 在 Python 中使用 Pefile 查找可执行文件的 DLL 的绝对路径
- javascript - 在每个函数调用上更改函数参数
- php - 在 WooCommerce 3+ 中将已付款订单标记为“已完成”,未付款为“已取消”
- recursion - 使用 Lisp 递归检查连续数字
- amazon-web-services - 可以在 Redshift 中的特定目录中创建外部表吗?
- regex - 为什么 Windows 上的 git commit-msg 与正则表达式不匹配
- java - 在java中获取python方法的结果返回意外结果
- python - Python比较数据框列并替换为基于前缀的内容
- excel - 如何在 excel 图中删除/添加特定的网格线?
- selenium - 如何使用 JavaScript 使用 selenium Web 驱动程序 API 定位后代元素?