【问题标题】:NSNotificationCenter: addObserver that is not selfNSNotificationCenter: addObserver 不是自己
【发布时间】:2014-11-22 15:54:39
【问题描述】:

问题来了:

在加载第二个视图之前,一个视图控制器能否将另一个视图控制器作为观察者添加到默认中心?

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

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

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

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

类似:

[[NSNotificationCenter defaultCenter] addObserver: TableViewController ...

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

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

来自 MapViewController 的 viewDidLoad:

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

【问题讨论】:

  • 可以,但您至少需要先实例化表格视图控制器并将该指针作为观察者传递。
  • 我会重构,使数据模型是一个单例。然后每个类都可以获得对数据模型的引用并注册通知

标签: ios objective-c nsnotificationcenter


【解决方案1】:

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

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

我刚刚测试了上面的代码,它对我来说运行良好,在我的详细视图控制器中调用了以下函数:

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

    NSLog(@"RECEIVED");
}

【讨论】:

  • 谢谢。我尝试在我的 MapViewController 的 viewDidLoad 方法中使用我的 TableViewController 的标识符来实现这个解决方案 - 它编译得很好,但加载时表仍然是空的。
  • 您是否在通知调用的函数中添加了一条日志语句并命中了它?
  • 我做到了。它没有击中它。我收到未声明选择器的警告...我指定的选择器是 TableViewController 正在使用的选择器...
  • 请在您的问题中发布更多代码,我不确定您到底在做什么。要摆脱未声明的选择器警告,您必须在表格视图控制器的头文件中声明它。
  • 注意警告,谢谢。我添加了代码 - 如果您需要更多,请告诉我。你看我在尝试你的解决方案时从 TableViewController 中注释掉了addObserver
猜你喜欢
  • 2014-07-25
  • 1970-01-01
  • 1970-01-01
  • 2012-12-07
  • 1970-01-01
  • 1970-01-01
  • 2015-05-01
  • 1970-01-01
  • 2014-11-11
相关资源
最近更新 更多