NSNotification postNotificationName在AppDelegate但NSNotificationCenter在ViewController? [英] NSNotification postNotificationName in AppDelegate but NSNotificationCenter in ViewController?

查看:269
本文介绍了NSNotification postNotificationName在AppDelegate但NSNotificationCenter在ViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法得到选择器方法,receiveChatText,在NSNotificationCenter执行,我想知道是否的问题是因为NSNotification postNotificationName是在AppDelegate.m但NSNotificationCenter是在ViewController.m? I.E.可以的postNotificationName知道NSNotificationCenter是在另一个viewController文件或者是我需要告诉它吗?



在viewController.m我有

   - (id)init 
{
self = [super init];
if(self){
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveChatText :)
name:ChatMessageReceived
object:nil];
return self;
}

- (void)receiveChatText:(NSNotification *)note {
NSLog(@received chat text);

}

在顶层AppDelegate.m文件中

   - (void)didReceiveMessage {
[[NSNotificationCenter defaultCenter] postNotificationName:ChatMessageReceived
object: nil
userInfo:nil];当调用didReceiveMessage时,可以阻止receiveChatText被执行的任何想法是什么?
p>

解决方案


我无法获取选择器方法,receiveChatText,...


首先,它是 receiveChatText:,带冒号。这在Objective-C - receiveChatText: receiveChatText 中不是指同样的方法很重要。



其次,selector并不意味着你的意思。选择器是方法的名称。您传递您希望通知中心发送给观察者的消息的选择器。也就是说,你告诉通知中心当这个通知到达时,向我发送一个 receiveChatText:消息给我[view controller]。


...在NSNotificationCenter ...


通知中心没有 receiveChatText:方法。你的观察者( self )会,这就是为什么你想让通知中心发送消息。


...执行,我想知道是否的问题是因为NSNotification postNotificationName在AppDelegate.m ...


没有这样的事情在AppDelegate.m。



应用程式委托发布通知。


...但NSNotificationCenter在ViewController.m中?


在ViewController.m中没有这样的东西。

视图控制器会观察通知。



只要视图控制器将自己添加为观察者,通知,这将工作。如果它不工作,一个或两个步骤没有发生或他们发生在错误的顺序。


I.E。可以的postNotificationName知道NSNotificationCenter是在另一个viewController文件或者是我需要告诉它吗?


通知中心isn' t在任一文件。 [NSNotificationCenter defaultCenter] 是一个单例对象,在整个应用程序中共享任何要使用它的对象。这是你能够使用它来让应用程序代理与视图控制器以及任何其他正在监视通知的对象通信。



您正在发送默认通知中心a postNotificationName:object:userInfo:消息。这是您应该以前发送过 addObserver:selector:name:object:消息的默认通知中心。只要您先开始观察,然后将通知发送到同一个通知中心,通知中心就可以向您添加的观察者分发通知。



< blockquote>

任何想法可以停止receiveChatText


receiveChatText:


在调用didReceiveMessage时执行?



b $ b


  1. didReceiveMessage 未发布通知。假设您在问题中显示的代码是准确的,则不是这样。

  2. 视图控制器没有遵守通知。因为它在创建时开始观察,或许它还没有创建。或者,这可能是因为您重写了 init ,而不是NS / UIViewController的 initWithNibName:bundle:。注意不同类的指定初始化器是什么;

  3. 视图控制器尚未观察到通知:您在视图控制器开始观察通知之前发布了通知(即,在创建视图控制器之前) 。

您可能还需要将聊天文本作为通知的对象或其 userInfo ,而不是强制通知的所有观察者从未指定的来源检索它。


I can't get the selector method, receiveChatText, in the NSNotificationCenter to execute and I am wondering if the problem is because the NSNotification postNotificationName is in AppDelegate.m but NSNotificationCenter is in ViewController.m? I.E. can the postNotificationName know that the NSNotificationCenter is in another viewController file or is that something I need to tell it?

In a viewController.m I have

 -(id)init
 {
self = [super init];
if(self){
    [[NSNotificationCenter defaultCenter] addObserver:self  
                                      selector:@selector(receiveChatText:) 
                                                      name:ChatMessageReceived  
                                               object:nil];
 return self;
}

- (void)receiveChatText:(NSNotification *)note {
NSLog(@"received chat text");

}

and in the top level AppDelegate.m file I have the following:

 -(void) didReceiveMessage {
    [[NSNotificationCenter defaultCenter] postNotificationName:ChatMessageReceived 
                                          object:nil
                                          userInfo:nil];        
 }

Any ideas what could stop receiveChatText from being executed when didReceiveMessage is called?

解决方案

I can't get the selector method, receiveChatText, …

First, it's receiveChatText:, with the colon. This is important in Objective-C —receiveChatText: and receiveChatText do not refer to the same method.

Second, "selector" does not mean what you think it means. A selector is the name of a method. You pass in the selector for the message you want the notification center to send to the observer. That is, you are telling the notification center "when this notification arrives, send a receiveChatText: message to me [the view controller]".

… in the NSNotificationCenter …

The notification center doesn't have a receiveChatText: method. Your observer (self) does, which is why that's what you want the notification center to send the message to.

… to execute and I am wondering if the problem is because the NSNotification postNotificationName is in AppDelegate.m …

No such thing is in AppDelegate.m.

The app delegate posts a notification.

… but NSNotificationCenter is in ViewController.m?

No such thing is in ViewController.m.

The view controller observes for a notification.

As long as the view controller adds itself as an observer before the app delegate posts the notification, this will work. If it doesn't work, either one or both steps did not happen or they happened in the wrong order.

I.E. can the postNotificationName know that the NSNotificationCenter is in another viewController file or is that something I need to tell it?

The notification center isn't in either file. [NSNotificationCenter defaultCenter] is a singleton object, shared throughout your entire application amongst any objects that want to use it. That's how you're able to use it to have the app delegate communicate to the view controller and any other objects that are observing for the notification.

You are sending the default notification center a postNotificationName:object:userInfo: message. This is the same default notification center that you should have previously sent an addObserver:selector:name:object: message. As long as you start observing first, then send the notification to the same notification center, the notification center will be able to dispatch the notification to the observer(s) you added.

Any ideas what could stop receiveChatText

receiveChatText:

from being executed when didReceiveMessage is called?

  1. didReceiveMessage did not post a notification. Assuming the code you showed in your question is accurate, this is not the case.
  2. The view controller is not observing for the notification. Since it starts observing on creation, perhaps it hasn't been created yet. Or, it may be because you've overridden init rather than NS/UIViewController's initWithNibName:bundle:. Be aware of what different classes' designated initializers are; the documentation will usually say.
  3. The view controller is not observing for the notification yet: You posted the notification before the view controller started observing for it (i.e., before you created the view controller).

You might also want to pass the chat text as the object of the notification, or in its userInfo, rather than forcing all observers of the notification to retrieve it from an unspecified source.

这篇关于NSNotification postNotificationName在AppDelegate但NSNotificationCenter在ViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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