我有一个UITableViewCell
ProductsCell
,我在nib中注册了它,并退出队列以使用它。
我想将其属性设置为只读。如何做得更好?
代码如下:
@property (nonatomic, strong, readonly) MyProductsVC * targetMyProductsVC;
- (MyProductsVC *)targetMyProductsVC{
if(!_targetMyProductsVC){
UIResponder *target = self.nextResponder;
do {
target = target.nextResponder;
} while (![target isKindOfClass: ZBMyProductsVC.self] && target != nil);
_targetMyProductsVC = (ZBMyProductsVC *)target;
}
return _targetMyProductsVC;
}
我无法实施。在里面
-init
,
-awakeFromNib
,因为我使用
UIResponder
查找父ViewController。
因为在上述方法中,单元格似乎没有添加到超级视图中。
如果我喜欢这个,
@property (nonatomic, strong, readonly) MyProductsVC * targetMyProductsVC;
Xcode报告:
使用未声明的标识符“\u targetMyProductsVC”
可以设置两个属性。一个在内部,如上所述,只需将另一个只读属性设置在外部
getter
方法(
return
以前的财产。)
有点脏,
还有更好的方法吗?
代码可以工作:
@property (nonatomic, strong, readonly) MyProductsVC * targetMyProductsVCReadOnly;
@property (nonatomic, strong) MyProductsVC * targetMyProductsVC;
- (MyProductsVC *)targetMyProductsVC{
if(!_targetMyProductsVC){
UIResponder *target = self.nextResponder;
do {
target = target.nextResponder;
} while (![target isKindOfClass: ZBMyProductsVC.self] && target != nil);
_targetMyProductsVC = (ZBMyProductsVC *)target;
}
return _targetMyProductsVC; }
- (MyProductsVC *)targetMyProductsVCReadOnly{
return self.targetMyProductsVC; }