2014-09-27 31 views
1

這裏的問題: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***********"); 
} 
+0

它可以但你需要至少實例化表視圖控制器,並將該指針作爲觀察者傳遞。 – rmaddy 2014-09-27 22:47:52

+0

我會重構,以便數據模型是一個單身人士。然後每個類可以獲取數據模型的引用並註冊通知 – Paulw11 2014-09-27 23:31:44

回答

0

這是可能的,只要你通過指針到您的視圖控制器作爲觀察員。這裏有一個例子:

//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"); 
} 
+0

謝謝。我試圖用我的TableViewController的標識符在MapViewController的'viewDidLoad'方法中實現這個解決方案 - 它編譯得很好,但是在加載時表仍然是空的。 – MayNotBe 2014-09-28 00:01:48

+0

您是否在通知調用的函數中放置了日誌語句,並且它是否觸及了它? – rebello95 2014-09-28 00:13:56

+0

我做到了。它沒有擊中它。我得到了一個未聲明的選擇器的警告......我指定的選擇器是TableViewController正在使用的選擇器... – MayNotBe 2014-09-28 00:34:44

相關問題