NSNotificationCenter的大问题,它不会更新目标的显示 [英] big problem with the NSNotificationCenter, it doesn't update the display of the target

查看:65
本文介绍了NSNotificationCenter的大问题,它不会更新目标的显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮助,我死了..由于这个原因,我无法继续我的应用程序:

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

我在2个可可Objective-C类别之间设置了一条消息, 主要appdelegate通过NSNotificationCenter传递视图 视图确实收到了通知,但是以某种方式无法更新视图上可见的控件.这就像找不到合适的实例之类.

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.

这是我的代码: mainapp(appdelegate在其中发送我的视图):

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

该函数在myuiviewcontoler

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..

如果我执行IBAction并将其分配给该视图的按钮并执行相同的操作: [self.mybutton setTitle:@"sdsdsdasd" forState:UIControlStateNormal]; 它将毫无问题地更新我的显示...修改标题...

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...

那么NSnotificationcenter调用的函数和属于同一视图的函数有什么区别...

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?

仍然不起作用...

几天前,我又发表了一篇文章..但没有人回答 以编程方式创建的标签和图像不会显示在屏幕上

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

推荐答案

听起来很像您的NewMessageNotification:方法没有在主线程上被调用; UI更新只能从主线程完成,否则将无法正常工作.

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
}

这篇关于NSNotificationCenter的大问题,它不会更新目标的显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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