以前 App 我們都是使用 xib 來寫

現在因為某個需求需要使用到 UISplitViewController

所以研究了一下如何從 Storyboard 頁面轉換到 xib 頁面

如果沒有牽扯到 UISplitViewController 只是簡單的從 Storyboard 抓頁面出來

這樣就可以了

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
MyTableViewController *myTableView = [storyboard instantiateViewControllerWithIdentifier:@"MyTableViewController"];

 

那如果有 UISplitViewController 情況就不同了

因為我們不是每個頁面都要 MasterDetail 只是某些頁面

所以就需要用到 presentViewController 這個 function

而 presentViewController 是會直接蓋在原本的頁面上

如果只有一頁可以這樣處理

MyViewController *myView = [[MyViewController alloc] initWithNibName:@"MyView" bundle:nil];
[self.view.window.rootViewController presentViewController:myView animated:YES completion:nil];

 

多個頁面則需要自訂 UINavigationController 來處理頁面轉換

MyNavigationController *myNavigationController = [[MyNavigationController alloc] initWithNibName:nil bundle:nil];
MyViewController *myView = [[MyViewController alloc] initWithNibName:@"MyView" bundle:nil];
[myNavigationController pushViewController:myView animated:FALSE];
[self.view.window.rootViewController presentViewController:myNavigationController animated:YES completion:nil];

 

另外還有就是要注意 presentViewController 需要在頁面出現後 (viewDidAppear) 才有作用

不然會遇到 presentViewController 執行了卻沒有出現畫面

Split view controller won't present modal view controller

arrow
arrow
    全站熱搜

    JohnDX 發表在 痞客邦 留言(0) 人氣()