NSNotificationCenter: addObserver which is not

Here's the question:

Is it possible for one view controller to add another view controller as an observer to the default center before loading the second view?

I have a model class that creates NSURLSession, captures some data, builds an array, and sends the notifications it made (along with a pointer to the array).

My applications are loaded using Map View, which creates an instance of the model, calls a method to create an array, listens for a notification, and drops contacts using the array.

I have a table tab that I want to load using an array built on a map.

Can my map view add my table observer as an observer before loading the table?

Sort of:

[[NSNotificationCenter defaultCenter] addObserver: TableViewController ...

Thank you for understanding. I understand this when I go.

----------------- EDIT --------------------

viewDidLoad from MapViewController:

- (void)viewDidLoad
{
  [super viewDidLoad];
  _mapView.delegate = self;
  _model = [[WikiModel alloc] init];
  _lastArticleUpdateLocation = [[CLLocation alloc] initWithLatitude:0 longitude:0];
  _lastUpdateUserLocation    = [[CLLocation alloc] initWithLatitude:0 longitude:0];

  // Listen for WikiModel to release updates.
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(reloadData:)
                                               name:@"Array Complete"
                                             object:_model];

//table view listener attempt ...
UITableViewController *tvc = [self.storyboard instantiateViewControllerWithIdentifier:@"tableViewController"];

[[NSNotificationCenter defaultCenter] addObserver:tvc
                                         selector: @selector(updateDataSource:)
                                             name:@"Array Complete"
                                           object:nil];
[self.navigationController pushViewController:tvc animated:YES];

}

From TableViewController:

- (void)viewDidLoad
{
  [super viewDidLoad];

  // Listen for WikiModel to release updates.
    /*
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(updateDataSource:)
                                               name:@"Array Complete"
                                             object:nil];*/
}

-(void)updateDataSource:(NSNotification *)notification
{
  _wikiEntries = [[notification userInfo] objectForKey:@"wikiEntryArray"];
  [self.tableView reloadData];
    NSLog(@"************received***********");
}
+1
source share
1 answer

This is possible if you pass the pointer to the view controller as an observer. Here is an example:

//Go to the detail view
VC2ViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"VC2ViewController"]; //ID specified in the storyboard
[[NSNotificationCenter defaultCenter] addObserver:dvc selector:@selector(notificationReceived:) name:@"MyNotification" object:nil];
[self.navigationController pushViewController:dvc animated:YES];

Then you can post your notice as usual:

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:nil];

I just tested the above code, and it worked fine for me, the following function was called in my detail view controller:

-(void)notificationReceived:(NSNotification *)notification {

    NSLog(@"RECEIVED");
}
0
source

All Articles