2017-01-30 42 views
1

我想讓ViewController F與ViewController C通信。 它們的安排如下: 導航控制器A嵌入ViewController B,它在容器視圖中呈現ViewController C. 從ViewController B到NavigationController D有一個segue,嵌入了ViewController E和ViewController F(在E和F之間延續)。兩個不直接連接的ViewControllers之間的通信

我當前工作的解決方案是:建立一個代表團的「路徑」必要ViewControllers之間:視圖控制器˚F代表們的ViewController E,委託給視圖控制器B,終於委託信息的ViewController C.
感覺就像必須有一個更簡單的方法來做到這一點。 你能推薦一個嗎?也許把「segue path」裏面的ViewController C移交給ViewController F來設置C和F之間的直接委託?

謝謝!

+0

是什麼'communicate'是什麼意思?運行方法?來回傳遞數據?迴應事件?你的問題不清楚。有許多可能的方法,如單身人士,委派,'NSNotificationCenter'。 –

+0

在我的情況下,溝通意味着:F需要能夠觸發C方法,並且還能傳遞數據。對不清楚的問題抱歉,謝謝指出! – seb

回答

2

我會用NSNotification

在視圖控制器F:

- (void)sendData { 
    // Fire the notification 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ReceivedData" 
                 object:nil]; 
} 

在視圖控制器C:

- (void)viewDidLoad { 
    [NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(receivedDataNotification:) 
               name:@"ReceivedData" 
               object:nil]; 
} 

- (void)receivedDataNotification:(id)object { 
    NSLog(@"Received Data!"); 
} 
+0

謝謝你指點我正確的方向!我現在使用'[[NSNotificationCenter defaultCenter] postNotificationName:@「ReceivedData」對象:self userInfo:userInfo];' – seb

相關問題