UITextView不显示文本

时间:2012-10-08 15:29:58

标签: uitextview

在我的viewDidLoad中,我可以让UILabels和UITextFields显示文本,但不能显示UITextView。我在下面的代码中做错了吗?

- (void)viewDidLoad {

[super viewDidLoad];

UIView* headerWrapper = [[UIView alloc] init];

//tap gesture is for each section so we can click on it
UITapGestureRecognizer *headerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showContent:)];

header = [[UIView alloc] initWithFrame: CGRectMake(10.0, 10.0, 738.0, 40.0)];
header.backgroundColor = [UIColor blackColor];

//get the header frame
CGRect headerFrame = header.frame;

//add the gesture
[headerWrapper addGestureRecognizer:headerTap];

//add a label
UILabel* lblSectionTitle = [[UILabel alloc] initWithFrame: CGRectMake(10.0, 3.0, headerFrame.size.width, 24.0)];
lblSectionTitle.text = @"This is a test";
lblSectionTitle.font = [UIFont fontWithName:@"Helvetica" size:20.0];
lblSectionTitle.textColor = [UIColor whiteColor];
lblSectionTitle.backgroundColor = [UIColor clearColor];
[header addSubview:lblSectionTitle];

[headerWrapper addSubview:header];

//this is the wrapper for the content
UIView* contentView = [[UIView alloc] initWithFrame:CGRectMake(10.0, headerFrame.size.height + 15.0, headerFrame.size.width, 200.0)];
contentView.backgroundColor = [UIColor redColor];

UITextView* tvContent = [[UITextView alloc] initWithFrame:CGRectMake(0.0, 0.0, headerFrame.size.width, 200.0)];
[tvContent setText:@"This is a content test."];
tvContent.textColor = [UIColor blackColor];
tvContent.backgroundColor = [UIColor clearColor];
tvContent.font = [UIFont fontWithName:@"Trebuchet" size:18.0];

[contentView addSubview:tvContent];
[headerWrapper addSubview:contentView];
[self.view addSubview:headerWrapper];

NSLog(@"%@", tvContent.text);

}

1 个答案:

答案 0 :(得分:1)

我今天遇到了这个问题并通过更改解决了这个问题:

[tvContent setText:@"This is a content test."];

tvContent.text = @"This is a content test.";

起初我认为问题在于没有加载我的UITextView。但是在我确定它已经在界面构建器中加载并连接之后(不是你的情况,因为你是以编程方式进行的),我只能通过直接归属而不是调用setText来设置其内容。

希望它有所帮助!