[英]Can I set the property for a class, which hasn't been specified until run in OC?
I've got a fixed controller with dynamic views as its view. 我有一个带有动态视图的固定控制器。 I want to set value for property of a certain view. 我想为某个视图的属性设置值。
Here's code in the controller as below: 这是控制器中的代码,如下所示:
@property (nonatomic, retain) Class viewClass;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view = _viewClass.new;
if ([_viewClass resolveInstanceMethod:@selector(lineAdded)]) {
[_viewClass setValue:@YES forKey:@"lineAdded"];
}
self.view.backgroundColor = [UIColor whiteColor];
}
In * the certain* view, I've got a property like this. 在*特定*视图中,我具有这样的属性。
@property (nonatomic, assign) BOOL lineAdded;
It reminds me 这让我想起
Undeclared selector 'lineAdded' 未声明的选择器“ lineAdded”
When I run, it just skip if condition and go on. 当我跑步时,只要有条件就跳过它,然后继续。
My question is: is it impossible to set property when the class it belongs to isn't specified? 我的问题是:在未指定属性所属类的情况下,是否可以设置属性?
Hope somebody could help me. 希望有人能帮助我。 Thanks in advance. 提前致谢。
When you are setting value of @YES
. 当您设置@YES
值时。 You should declare that as NSNumber
instead of using primitive data type: BOOL
. 您应该将其声明为NSNumber
而不要使用原始数据类型: BOOL
。 When you retrieve, you should use -(BOOL)boolean
method to retrieve it. 检索时,应使用-(BOOL)boolean
方法检索它。 As for the resolveInstanceMethod
, I think you should look into this documentation to make sure that's the correct logic you are putting in. 至于resolveInstanceMethod
,我认为您应该研究本文档以确保您所输入的逻辑正确。
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtDynamicResolution.html https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtDynamicResolution.html
You can get rid of the warning by making the compiler see a declaration of the lineAdded
selector. 您可以通过使编译器看到lineAdded
选择器的声明来摆脱警告。 Either #import
a header file that contains a declaration of that property, or declare it another way, for example like this: #import
包含该属性声明的头文件,或以另一种方式声明它,例如:
@protocol DummyProtocol
@property (nonatomic, unsafe_unretained) BOOL lineAdded;
@end
Second, setting the value of the property doesn't require the lineAdded
selector. 其次,设置属性的值不需要lineAdded
选择器。 It requires the setLineAdded:
selector. 它需要setLineAdded:
选择器。
Third, the proper way to check whether self.view
responds to setLineAdded:
is to ask it, like this: 第三,正确的方法来检查是否self.view
响应setLineAdded:
是问它,就像这样:
if ([self.view respondsToSelector:@selector(setLineAdded:)]) {
[self.view setValue:@YES forKey:@"lineAdded"];
}
Fourth, you were asking _viewClass
whether its instances respond to lineAdded
, but then you were asking the _viewClass
object itself (rather than the self.view
instance of it) to set its own lineAdded
property. 第四,您要询问_viewClass
其实例是否响应lineAdded
,但是随后您要询问_viewClass
对象本身(而不是其self.view
实例)来设置其自己的lineAdded
属性。 I fixed that in my code above. 我在上面的代码中修复了该问题。
Fifth, you should assign to self.view
in loadView
, not in viewDidLoad
. 第五,您应该在loadView
中而不是viewDidLoad
分配给self.view
。
If, after all that, it's not setting lineAdded
, then your view (whatever class you chose) simply doesn't respond to setLineAdded:
. 毕竟,如果没有设置lineAdded
,那么您的视图(无论选择setLineAdded:
类)都不会响应setLineAdded:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.