0

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
PartySoft
  • 2,749
  • 7
  • 39
  • 55
  • 1
    Also, where are you calculating calculatedHeight and lastcalculatedHeight? – Chris Allwein Mar 23 '11 at 17:55
  • yes i'm doing that, it all shows on the screen, if I call the function from an event of a button touch up inside it updates and I can see what it ads, ...if i do it through NSNotificationCenter it doesn't work...it calls the function but it doesn't post any visible updates... – PartySoft Mar 23 '11 at 21:03

1 Answers1

0

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.

Community
  • 1
  • 1
Anomie
  • 92,546
  • 13
  • 126
  • 145