NSNotificationCenter:添加不是自己的观察者 [英] NSNotificationCenter: addObserver that is not self

查看:83
本文介绍了NSNotificationCenter:添加不是自己的观察者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题来了:

在加载第二个视图之前,一个视图控制器可以添加另一个视图控制器作为观察者到 defaultCenter 吗?

Can one View Controller add another View Controller as an Observer to the defaultCenter before the second view has been loaded?

我有一个模型类,它创建一个 NSURLSession,获取一些数据,构建一个数组,并发送它完成的通知(以及一个指向数组的指针).

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

我的应用加载了一个地图视图,该视图实例化模型、调用方法来创建数组、侦听通知并使用数组放置引脚.

My app loads with a Map View that instantiates the model, calls the method to create the array, listens for the notification, and drops pins using the array.

我有一个表视图选项卡,我想使用地图构建的数组加载它.

I have a Table View tab that I want to load using the array built by the map.

在加载 Table View 之前,我的 Map View 可以添加我的 Table View Controller 作为观察者吗?

类似于:

[[NSNotificationCenter defaultCenter] addObserver: TableViewController ...

感谢您的任何见解.我边走边想.

Thanks for any insight. I'm figuring this out as I go.

-----------------编辑--------------------

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

来自 MapViewController 的 viewDidLoad:

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

}

来自 TableViewController:

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

推荐答案

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

This is possible as long as you pass the pointer to your view controller as the observer. Here's 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 notification 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");
}

这篇关于NSNotificationCenter:添加不是自己的观察者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆