编程方式创建的标签和图像不显示在屏幕上
问题描述:
我有以下的应用程序,这是想聊天,所以我编程创建一些标签,图像和按钮。
问题是,我在启动时添加的那个显示,而我之后添加的那个不显示。
这就像视图需要清爽或什么。 如果我把从设定到按钮的IBAction为相同的绘图功能......它显示的内容.. 如果它来自于寻找新的活动,并通过通知类线程事件:编程方式创建的标签和图像不显示在屏幕上
[[NSNotificationCenter defaultCenter] postNotificationName:@"NewMessageNotification" object:self userInfo:values];
它然后不显示任何东西。 但是这个函数实际上被调用了(我用调试器进行了检查)它是不是使用视图的正确实例?
这里是我的布局:
,这里是我的函数创建该控件:
UILabel *labelMessage = [[UILabel alloc] initWithFrame:CGRectMake(left+5, lastcalculatedHeight-5, 220, calculatedHeight)];
[labelMessage setText:msg];
[labelMessage setFont:[UIFont fontWithName:@"Arial" size:12.0]];
[labelMessage setLineBreakMode:UILineBreakModeWordWrap];
[labelMessage setTextColor:[UIColor blackColor]];
[labelMessage setAdjustsFontSizeToFitWidth:NO];
[labelMessage setNumberOfLines:floor(msg.length/40)+2];
[labelMessage setBackgroundColor:[UIColor clearColor]];
[scrollView addSubview:labelMessage];
[labelMessage release];
UIButton *buttonTime = [[UIButton alloc] initWithFrame:CGRectMake(left, lastcalculatedHeight+40, 50, 20)];
[buttonTime setBackgroundImage:[[UIImage imageNamed:@"hour_bubble.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:13] forState:UIControlStateDisabled];
[buttonTime setFrame:CGRectMake(left_hour_button, lastcalculatedHeight+calculatedHeight-30, 55, 25)];
[buttonTime setTitle:date2 forState:UIControlStateDisabled];
[buttonTime setTitleColor:[UIColor blackColor] forState:UIControlStateDisabled];
buttonTime.titleLabel.font=[UIFont fontWithName:@"Helvetica" size:8.0];
buttonTime.titleLabel.lineBreakMode= UILineBreakModeWordWrap;
[buttonTime setEnabled:FALSE];
[scrollView addSubview:buttonTime];
[buttonTime release];
我也想给UIScrollView的自动滚动到下的时候我把这个功能..但它失败了,这就像模拟器快要疯了,当我拨打以下: 我尝试使用
CGPoint offset = CGPointMake(0,lastcalculatedHeight);
[scrollView setContentOffset: offset animated: YES];
答
你描述听起来像没有被主线程发送您的通知的行为;所有UI更新必须在主线程上完成。请参阅my answer to your other question了解解决该问题的一些代码。
+0
这真的工作:)这太棒了!谢谢你的回应10倍 – PartySoft 2011-04-03 01:21:21
另外,你在哪里计算calculatedHeight和lastcalculatedHeight? – 2011-03-23 17:55:30
是的,我正在这样做,它全部显示在屏幕上,如果我从一个按钮的事件调用函数触摸它的内部更新,我可以看到它的广告,...如果我通过NSNotificationCenter做它它doesn没有工作......它调用的功能,但它不会发布任何可见的更新... – PartySoft 2011-03-23 21:03:34