2015-04-02 82 views
0

我嘗試從故事板得到的ViewController是這樣的:如何通過標識符在故事板中獲取視圖控制器?

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
    ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"vc"]; 
    ViewController *avc = ((UINavigationController *)(_window.rootViewController)).topViewController; 

但它們不是同一個對象:

(lldb) po vc <ViewController: 0x17eaee50> 
(lldb) po avc <ViewController: 0x17e72970> 

爲什麼?

+0

'vc'是一個新對象,而'avc'是一個已經創建的對象。 – Larme 2015-04-02 18:22:32

+1

當你說「試圖獲得一個視圖控制器」時,你的意思是,你試圖獲得一個已經實例化的指針,或者你試圖實例化一個新的? – rdelmar 2015-04-02 18:24:33

+0

哦!我不知道** instantiateViewControllerWithIdentifier **會創建一個新的... – bazysong 2015-04-02 18:28:27

回答

1

它們屬於同一類別,但具有不同的實例。 instantiateViewControllerWithIdentifier實例化一個新的視圖控制器,顧名思義:)。

+0

我犯了一個錯誤...任何方式從故事板獲取已創建的視圖控制器? – bazysong 2015-04-02 18:30:27

+0

不,不是從故事板。如何獲取控制器取決於您的控制器和視圖結構。 – Rengers 2015-04-02 19:36:43

相關問題