0

我有以下应用程序,就像聊天一样,所以我正在以编程方式创建一些标签、图像和按钮。
问题是我在启动时添加的那些会显示,而我之后添加的那些则不会。
这就像视图需要刷新或什么的。如果我从 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];

以编程方式从 UIScrollView 滚动到底部得到了这个

4

1 回答 1

0

您描述的行为听起来像是您的通知没有在主线程上发送;所有 UI 更新都必须在主线程上完成。请参阅我对您的其他问题的回答,以获取一些解决该问题的代码。

于 2011-03-26T19:01:34.293 回答