storyboard 如何用代碼調用

轉自網絡:網絡

 

一、獲得一個storyboard對象(UIStoryBoard類的對象)。ui

若是你有一個已經存在的view controller,並且這個view controller是從同一個storyboard實例化而來的,那麼能夠經過它的storyboard屬性得到storyboard對象。要加載另一個storyboard對象,能夠調用UIStoryboard的類方法storyboardWithName:bundle: ,並將storyboard文件的文件名和一個可選的bundle做爲參數傳給該方法。spa

 

二、調用storyboard對象的instantiateViewControllerWithIdentifier: 方法,將你在Interface Builder中建立view controller時定義的標識符傳給這個方法。設計

或者,你能夠使用instantiateInitialViewController方法來實例化storyboard的首個view controller,而不用指定標識符。code

 

三、顯示這個新的view controller。對象

==============it

一下用代碼解釋更清楚:io

==============class

如何在代碼中,調用一個已經在storyboard中設計好的scene呢?代碼以下:方法

NSString * storyboardName = @"MainStoryboard_iPhone"; NSString * viewControllerID = @"ViewID"; UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil]; MyViewController * controller = (MyViewController *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID]; [self presentViewController:controller animated:YES completion:nil];
相關文章
相關標籤/搜索