2014-01-28 103 views
3

我不知道這是否可行,但我用xibs編寫了一個項目,並希望以模態方式切換到故事板項目。從UIViewcontroller調用故事板

我知道切換VC的,你這樣做

NewViewController *NewVC = [[NewViewController alloc] init]; 
[self presentModalViewController:NewVC animated:YES]; 

在點擊一個按鈕,我會打電話給「開關」

有僅使用,我想打電話給故事板另一個項目使用「開關」

我該如何去做這件事?是否有可能做到這一點?

+0

尋找'instantiateViewControllerWithIdentifier'。並且,請使用小寫字母表示變量的第一個字母(這裏是NewVC到newVC)。 – Larme

+0

謝謝,未來會做。 –

回答

5

是的,這是可能的。你可以做這樣的事情:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YourStoryboardName" bundle:nil]; 
MainMenuViewController *mainmenuvc = [storyboard instantiateViewControllerWithIdentifier:@"mainMenuViewController"]; 
[self.navigationController pushViewController:mainmenuvc animated:YES]; 

編輯 如果我理解你的評論,你想改變根視圖控制器。你可以這樣做:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"RVCStoryboard" bundle:nil]; 
MainMenuViewController *mainmenuvc = [storyboard instantiateViewControllerWithIdentifier:@"mainMenuViewController"]; 
[[UIApplication sharedApplication].delegate].window.rootViewController = mainmenuvc; 

請注意,這個工作,你RVCStoryboard你必須選擇你要加載的視圖控制器,並分配一個名稱,它使得instantiateViewControllerWithIdentifier:方法會奏效。檢查附加的圖像,字段「Storyboard ID」: enter image description here

+0

謝謝你的幫助!我想我應該更清楚。我有一個VC,RootViewController,我想去哪個使用我的故事板RVCStoryboard.storyboard。如果我可以得到你正確工作的代碼,這是否從VC(這是xib)「開關」按鈕開啓到RootViewController/RVCStoryboard? –

+0

故事板中包含一個導航控制器和一個根視圖控制器,其中有一個表格視圖。這會有所作爲嗎? –

+0

@ VC9結帳我的編輯請。試試代碼,它應該工作。 –

相關問題