以编程方式创建的标签和图像不会显示在屏幕上 [英] Programmatically created labels and images don't show on screen

查看:51
本文介绍了以编程方式创建的标签和图像不会显示在屏幕上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下应用,就像聊天一样,所以我正在以编程方式创建一些标签,图像和按钮.
问题在于,我在启动时添加的内容确实显示,而我之后添加的内容却不显示.
就像视图需要刷新一样.如果我从设置为按钮的IBAction调用相同的绘图函数,则会显示内容.如果它来自寻找新事件并通过以下方式通知该类的线程事件:

I have the following app, that's like a chat, so I'm programmatically creating some labels, images and buttons.
The problem is that the ones that I'm adding at startup do show and the ones that I'm adding afterwards don't.
It's like the view needs refreshing or something. If I call the same drawing function from an IBAction set to a button...it displays the content.. if it comes from the thread event that looks for new events and notifies the class through:

[[NSNotificationCenter defaultCenter] postNotificationName:@"NewMessageNotification" object:self userInfo:values];  

然后它什么都不显示.但是实际上调用了该函数(我已经与调试器进行了检查)可能是它没有使用视图的正确实例吗?

it then doesn't show anything. But the function is actually called (I've checked with the debugger) Could it be that it doesn't use the proper instance of the view?

这是我的布局:

这是我创建控件的函数:

and here is my function that creates the controls:

    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自动滚动到底部.但是它失败了,就像当我调用以下命令时模拟器正在发疯一样:我尝试使用

I also want to autoscroll the uiscrollview to bottom when I call this function..but it fails, it's like the simulator is going crazy when I call the following: I tried using

CGPoint offset = CGPointMake(0,lastcalculatedHeight);
[scrollView setContentOffset: offset animated: YES];

我从这里得到的 UIScrollView以编程方式滚动到底部

推荐答案

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

The behavior you describe sounds like your notification is not being sent on the main thread; all UI updates must be done on the main thread. See my answer to your other question for some code to work around the problem.

这篇关于以编程方式创建的标签和图像不会显示在屏幕上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆