1

在我的 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);

}
4

1 回答 1

1

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

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

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

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

希望能帮助到你!

于 2012-10-08T15:47:02.070 回答