【发布时间】: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