【问题标题】:Programmatically created labels and images don't show on screen以编程方式创建的标签和图像不会显示在屏幕上
【发布时间】:2011-07-21 12:22:46
【问题描述】:

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

【问题讨论】:

  • 另外,你在哪里计算计算的Height和lastcalculatedHeight?
  • 是的,我正在这样做,它都显示在屏幕上,如果我从一个按钮触摸事件中调用该函数,它会更新并且我可以看到它的广告,...如果我通过 NSNotificationCenter 执行它不起作用...它调用该函数但它没有发布任何可见的更新...

标签: iphone uiscrollview autoscroll


【解决方案1】:

您描述的行为听起来像是您的通知没有在主线程上发送;所有 UI 更新都必须在主线程上完成。请参阅my answer to your other question 获取一些解决问题的代码。

【讨论】:

  • 这真的很管用 :) 太棒了!感谢您的回复 10x
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-28
  • 2017-01-07
  • 1970-01-01
  • 1970-01-01
  • 2014-04-02
  • 2012-11-19
相关资源
最近更新 更多