NSNotificationCenter:不是自我的addObserver

这是一个问题:

在第二个视图加载之前,一个View Controller能否将另一个View Controller作为Observer添加到defaultCenter中?

我有一个模型类创build一个NSURLSession ,抓取一些数据,build立一个数组,并发送通知它已经完成(以及一个指向数组的指针)。

我的应用程序加载了一个实例化模型的Map View,调用创build该数组的方法,侦听通知,并使用该数组删除引脚。

我有一个表格视图选项卡,我想加载使用由地图build立的数组。

我的地图视图可以在加载表视图之前将我的表视图控制器添加为观察者?

就像是:

 [[NSNotificationCenter defaultCenter] addObserver: TableViewController ... 

感谢您的任何见解。 我正在考虑这个问题。

– – – – – – – – -编辑 – – – – – – – – – –

viewDidLoad来自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]; 

}

从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***********"); } 

只要您将指针传递给您的视图控制器作为观察者,这是可能的。 这是一个例子:

 //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]; 

然后,您可以照常发布您的通知:

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

我刚刚testing了上面的代码,它对我来说工作的很好,在我的详细视图控制器中调用了以下函数:

 -(void)notificationReceived:(NSNotification *)notification { NSLog(@"RECEIVED"); }