【发布时间】:2016-01-27 11:56:11
【问题描述】:
我正在使用 NSNotificationCenter 从继承自 NSObject 的类发送通知。
通知应该发送到 2 个 viewController 但它只发送到其中一个。
我的代码:
fetchFromParse:
-(void)sendAllStores
{
[[NSNotificationCenter defaultCenter]postNotificationName:@"getStoresArrays" object:nil userInfo:self.storesDict];
}
firstVC.m(工作中):
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getStoresArrays:) name:@"getStoresArrays" object:nil];
}
-(void)getStoresArrays:(NSNotification*)notification
{
NSLog(@“Working”); //Working
}
secondVC.m(不工作):
-(void)prepareArrays
{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getStoresArrays:) name:@"getStoresArrays" object:nil];
}
-(void)getStoresArrays:(NSNotification*)notification
{
NSLog(@“Not Working”); //Not working
}
AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
secondVC *secVC=[[secondVC alloc] init];
[secVC prepareArrays];
fetchFromParse *fetchFromParseObj=[[fetchFromParse alloc] init];
[fetchFromParseObj getStoresFromParse];
Return YES;
}
注意:Xcode 向我显示“firstVC 未注册为观察者”的错误消息。
【问题讨论】:
-
是否调用了firstVC的viewDidLoad?
-
@User31 是的,通知也正在发送到 firstVC,问题是 secondVC。
-
你会从 firstVC 中删除观察者吗?
-
@User31 不,我没有。
-
你应该从两个控制器中删除观察者
标签: ios objective-c iphone cocoa-touch nsnotificationcenter