postNotificationName不调用observer方法

spa*_*ker 9 iphone xcode nsnotificationcenter

我试图使用NSNotificationCenter从AppDelegate调用uiview中的方法无济于事.

AppDelegate.m

    [[NSNotificationCenter defaultCenter] postNotificationName:@"ProcessDidComplete" object:items];
Run Code Online (Sandbox Code Playgroud)

然后通过MainStoryboard,加载主视图,哪个控制器类是MainViewController

在MainViewController.h中我有viewDidLoad

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ProcessDidComplete:) name:@"ProcessDidComplete" object:nil];
Run Code Online (Sandbox Code Playgroud)

然后是方法

- (void) ProcessDidComplete:(NSNotification *)pNotification
Run Code Online (Sandbox Code Playgroud)

但它永远不会被召唤.

谢谢你的帮助!

Ram*_*ams 15

只是改变方式..

首先添加观察者

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ProcessDidComplete:) name:@"ProcessDidComplete" object:nil];
Run Code Online (Sandbox Code Playgroud)

然后发布通知

 [[NSNotificationCenter defaultCenter] postNotificationName:@"ProcessDidComplete" object:items];
Run Code Online (Sandbox Code Playgroud)

最后在viewWillDisappear中删除

 [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ProcessDidComplete" object:nil];
Run Code Online (Sandbox Code Playgroud)


Ste*_*ton 5

你的代码看起来没问题,这让我想知道你的应用委托你在哪里发布通知?

如果在视图控制器中添加观察者之前发布通知,则永远不会收到通知.您是否可以直接将消息发送到主视图控制器,即作为属性,而不是使用通知?