NSNotificationCenter 的大问题,它不更新目标的显示

发布于 2024-10-27 16:02:32 字数 1727 浏览 1 评论 0原文

救命,我很绝望..我无法继续我的应用程序,因为:

我已经在我的两个可可 Objective-C 类之间设置了一条消息, 主要的 appdelegate 通过 NSNotificationCenter 发送视图消息 视图确实收到了通知,但不知何故它无法更新视图上可见的控件..就像它找不到正确的实例或其他东西一样。

这是我的代码: mainapp(appdelegate 向我的视图发送消息的地方):

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

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

该函数是在 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!!! 
}

那么这可能是什么问题呢?使用此功能时,无法以编程方式修改任何可见内容,并且我需要这样做。

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

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

我试图查看在这两种情况下都是 self 变量,结果是相同的...... 所以..基本上使用相同的指针,对吧?

仍然不起作用...

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

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

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

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

发布评论

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

评论(2

奢华的一滴泪 2024-11-03 16:02:32

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

对于这种情况,通常的习语是这样的:

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

    // Existing code goes here
}

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
}
原来是傀儡 2024-11-03 16:02:32

听起来您的类可能有两个不同的实例,其中之一连接到笔尖中的所有内容,其中之一正在注册通知。尝试在该方法中记录 self ,当通知触发与按钮触发时,您可能会看到不同的对象地址。

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.

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