以编程方式创建的标签和图像不会显示在屏幕上

发布于 2024-10-26 14:33:35 字数 2255 浏览 1 评论 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 以编程方式滚动到底部

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?

here's my layout:

enter image description here

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];

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];

I got this from
UIScrollView scroll to bottom programmatically

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

地狱即天堂 2024-11-02 14:33:35

您所描述的行为听起来像是您的通知没有在主线程上发送;所有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文