开发者

big problem with the NSNotificationCenter, it doesn't update the display of the target

开发者 https://www.devze.com 2023-02-20 09:43 出处:网络
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,

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 associat开发者_开发百科ed 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


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
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消