這裏的問題:NSNotificationCenter:的addObserver不是自我
可以在一個視圖控制器添加其他視圖控制器作爲觀察員第二種觀點之前defaultCenter已經加載?
我有一個模型類創建NSURLSession
,抓取一些數據,構建一個數組,併發送它已完成的通知(以及指向數組的指針)。
我的應用程序加載了一個實例化模型的Map View,調用創建該數組的方法,偵聽通知,並使用該數組丟棄引腳。
我有一個表格視圖選項卡,我想使用由地圖建立的數組加載。
在加載表視圖之前,我的地圖視圖可以將我的表視圖控制器添加爲觀察者嗎?
喜歡的東西:
[[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***********");
}
它可以但你需要至少實例化表視圖控制器,並將該指針作爲觀察者傳遞。 – rmaddy 2014-09-27 22:47:52
我會重構,以便數據模型是一個單身人士。然後每個類可以獲取數據模型的引用並註冊通知 – Paulw11 2014-09-27 23:31:44