-2

我正在尝试在 rood VC 的 viewdidload 上提供另一个自定义视图。

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 中看到 textview。我认为我没有正确地做属性。任何帮助或解释?

提前致谢。

4

1 回答 1

3

您没有创建UIImageViewand 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;
}

您还需要将它们添加到某处的父视图中。

于 2013-03-15T21:28:25.310 回答