Ios 属性访问器方法实现
我正试图在rood VC的viewdidload上提供另一个自定义视图Ios 属性访问器方法实现,ios,objective-c,properties,Ios,Objective C,Properties,我正试图在rood VC的viewdidload上提供另一个自定义视图 MSHView *customTextView = [[MSHView alloc] initWithFrame:self.view.bounds]; customTextView.textView.text = @"abc"; [customTextView layoutSubviews]; [self.view addSubview:customTextView]; MSHView头文件: @property(nona
MSHView *customTextView = [[MSHView alloc] initWithFrame:self.view.bounds];
customTextView.textView.text = @"abc";
[customTextView layoutSubviews];
[self.view addSubview:customTextView];
MSHView头文件:
@property(nonatomic, strong) UITextView * textView;
@property (nonatomic, strong) UIImageView* translucentIV;
MSHView实现文件:
-(UIImageView*) translucentIV {
if(!_translucentIV) {
_translucentIV.frame = self.frame;
//NSLog(@"frame for translucent IV, %@", _translucentIV.frame);
_translucentIV.backgroundColor = [UIColor whiteColor];
_translucentIV.alpha = 0.5;
}
return _translucentIV;
}
-(UITextView* ) textView{
if(!_textView){
/*
float height = self.bounds.size.height - 5;
float width = self.bounds.size.width - 5;
_textView.frame = CGRectMake(5,5,width,height);
_textView.text = @"test text";
*/
NSLog(@"textview property being initialized");
}
return _textView;
}
-(void)layoutSubviews {
self.textView.frame = self.frame;
self.translucentIV.backgroundColor = [UIColor whiteColor];
self.alpha = 0.5;
NSString *abc = @"abc";
self.textView.text = [NSString stringWithFormat:@"%@",abc];
NSLog(@"layout subview being called");
}
-(void) setTextView:(UITextView *)textView {
if(textView != _textView) {
_textView = textView;
_textView.text = @"setter method called";
}
}
仍然无法从MSHView查看文本视图。我想我做得不好。有什么帮助或解释吗
提前谢谢 您没有创建
UIImageView
和UITextView
尝试更类似于:
-(UIImageView*) translucentIV {
if(!_translucentIV) {
_translucentIV = [[UIImageView alloc] initWithFrame:self.frame];
//NSLog(@"frame for translucent IV, %@", _translucentIV.frame);
_translucentIV.backgroundColor = [UIColor whiteColor];
_translucentIV.alpha = 0.5;
}
return _translucentIV;
}
-(UITextView *)textView {
if(!_textView) {
float height = self.bounds.size.height - 5;
float width = self.bounds.size.width - 5;
_textView = [[UITextView alloc] initWithFrame:CGRectMake(5,5,width,height)];
}
return _textView;
}
您还需要将它们添加到父视图的某个位置。您实际上没有将已分配的初始化实例分配给
\u translicentiv
和\u textView
变量。如果我做[self addSubView:_translucentIV],它们将保持在那里nil
;那不行试试[self addSubView:self.translucentIV]谢谢。这很有帮助。我走了另一条路,但你给我指明了正确的方向。我基本上必须分配并添加到子视图中。