2

Help, I'm deperate..i can't continue my app because of this:

I have setup one message between 2 of my cocoa objective-c clases, the main appdelegate is messaging a view through NSNotificationCenter the view receives indeed the notification but somehow it can't update the controls visible on the view.. it's like it can't find the proper instance or something.

here's my code: mainapp (where the appdelegate messages my view):

helloKey = @"0";
helloString = @"mymessage";

values = [NSMutableDictionary dictionaryWithObject:helloString forKey:helloKey];
[[NSNotificationCenter defaultCenter] postNotificationName:@"NewMessageNotification" object:self userInfo:values];

the function is defined in myuiviewcontoler

- (void)NewMessageNotification:(NSNotification *)notification
{

    // key associated with string "Hello String"
    helloKey = @"0";

    // [notificaiton userInfo] returns "values"
    helloFromMain = [[notification userInfo] objectForKey:helloKey]; 
    NSLog(@"newmess=%@",helloFromMain; //this outputs mymessage , perfect right?? not quite..here's the problem
        //then if I'm trying to update something visible ..it won't do IT!!!! :( :(
     //tested with the debugger and it reaches this line...when the messaging occurs
    [self.mybutton setTitle:@"sdsdsdasd" forState:UIControlStateNormal];
        //this won't work, not it will give an error!!! 
}

So what could it be the problem? nothing that is visible can be modified programaticaly when using this, and I kid of need that..

If I do a IBAction and assign it to a button of this view and do the same : [self.mybutton setTitle:@"sdsdsdasd" forState:UIControlStateNormal]; it will update my display without no problem... modifying the title...

So what's the difference between the function that is called by the NSnotificationcenter and a function that belongs to the same view...

I tried to see the address of the self variable in both cases,, it results to be the same... so ..basically uses the same pointer right?

still it doesn't work...

I've put another post back a few days ago..but no-one answered Programatically created labels and images don't show on screen

Community
  • 1
  • 1
PartySoft
  • 2,749
  • 7
  • 39
  • 55
  • What is the value of self.mybutton at that point? – Hollance Mar 25 '11 at 22:15
  • ok I will tell you in both cases, I can view it with the debuger – PartySoft Mar 25 '11 at 22:18
  • It I hover upon mybutton [self.mybutton setTitle:@"sdsdsdasd" forState:UIControlStateNormal]; it sais: UiRoundedrectbutton * mybutton 0x5c2a670 in the NewMessageNotification and if I probe it into another function from which it works it displays the same value: - (IBAction) DoTest: (id)sender { [self.mybutton setTitle:@"sdsdsdasd" forState:UIControlStateNormal]; } – PartySoft Mar 25 '11 at 22:41

2 Answers2

3

It sounds very much like your NewMessageNotification: method is not being called on the main thread; UI updates must only be done from the main thread, or things won't work correctly.

The usual idiom for a situation like this is something along these lines:

- (void)NewMessageNotification:(NSNotification *)notification
{
    if (![NSThread isMainThread]) {
        [self performSelectorOnMainThread:@selector(NewMessageNotification:) withObject:notification waitUntilDone:NO];
        return;
    }

    // Existing code goes here
}
Anomie
  • 92,546
  • 13
  • 126
  • 145
  • ok I will try this, and paste here how it goes, .. yes I was calling this from a thread that I've created for http requests..and I was calling this from there.. now I'm calling this once it gives control to the main thread, and what happends is: calls the function and after.. 4 seconds it reflects on the display also.. so that's acceptable.. – PartySoft Mar 28 '11 at 08:44
0

It sounds like you probably have two different instances of your class, one of which is hooked up to all the stuff in the nib and one of which is registering for the notification. Try logging self in that method and you'll probably see different object addresses when it's triggered by the notification versus the button.

Chuck
  • 234,037
  • 30
  • 302
  • 389
  • Yes it might seem that way, but I can asure you that self uses the same address in both (pointer is the same), that's why is so strange, because at first I thought the same. Could it be the simulator doing that? I can swear that one time it updated my display and then it didn't and it's kind of random... I don't have an iphone to test it... – PartySoft Mar 25 '11 at 22:46
  • yes, confirmed, it does modify the display but with a big delay..and random.. eighter is the simulator fault..or it's needed to forcely update the display somehow... – PartySoft Mar 25 '11 at 22:52