Reputation: 401
I am sending a notification from a class inherits from NSObject using NSNotificationCenter.
The notification should be sent to 2 viewController but it's being sent just to one of them.
My Code:
fetchFromParse:
-(void)sendAllStores
{
[[NSNotificationCenter defaultCenter]postNotificationName:@"getStoresArrays" object:nil userInfo:self.storesDict];
}
firstVC.m (working):
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getStoresArrays:) name:@"getStoresArrays" object:nil];
}
-(void)getStoresArrays:(NSNotification*)notification
{
NSLog(@“Working”); //Working
}
secondVC.m (not working):
-(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;
}
Note: Xcode shows me an error message that "firstVC is not registered as an observer".
Upvotes: 1
Views: 693
Reputation: 279
The logic for your code is not correct. You are posting a notification and have made two classes as observer, so this will lead to unpredictable results. You should have one class as observer for a notification.
Upvotes: 0
Reputation: 895
From what i see only one notification listener should be called here and that should be your SecondVC because your first view controller is not loaded yet so no observer is registered for first view controller.
Upvotes: 0
Reputation: 101
The firstVC is never used so it cannot add an observer then.
It adds the observer in the viewDidLoad, but if the view controller is never used it cannot load a view and therefore not add an observer.
Upvotes: 2