如果你曾經使用 interface builder 創建過一個復雜、界面非常多的應用,你就會明白最后那些Storyboards 文件變的有多大。他會迅速變的無法管理,阻礙你的進度。自從引入 Storyboard 以來,其實是可以把你的應用的不同模塊切開到不同的 Storyboard 中的。在過去,這要手動創建多個 Storyboard 文件,并且要寫大量的代碼。
為了解決這個問題,在 iOS 9 中蘋果介紹了 Storyboard References 這個概念。Storyboard References 允許你從 segue 中引用其他 storyboard 中的 viewController。這意味中你可以保持不同功能模塊化,同時 Storyboard 的體積變小并易與管理。不僅容易理解了,和團隊一起工作時,合并(工作成果)也變的簡單了。 --摘自
如上圖,這是一個簡單的Storyboard,正常的項目中如果使用的Storyboard的話,要遠遠比這個復雜的多.總共分三種方式(圖中的1 , 2 , 3),逐步深入學習使用Storyboard References.
方式1 簡化 Storyboard
1-1 選中紅色區域(高亮顯示)
1-2 點擊 Xcode 的菜單欄,選擇"Editor->Refactor to Storyboard"
1-3 命名為FristSB.storyboard
1-4 點擊Save之后就能看到FristSB.storyboard創建成功,選中 FristStoryBoard,查看其屬性.
1-5 雙擊FristStoryBoard,進入FristSB.storyboard.
Main.storyboard 中創建的 Storyboard References 的名字是由其 Referenced ID 這個標識符決定的. 進入 FristSB.storyboard 會發現 Storyboard ID 與 標識符相同.
方式2 簡化 Storyboard
2-1 創建單獨的storyboard,命名為 SecondSB.storyboard
2-2 找到“Storyboard Reference”控件,拖拽一個到 Contacts Storyboard上來. 選取剛剛創建的SecondSB,并設置標識符.
2-3 進入 SecondSB.storyboard,在 Storyboard ID 設置相同的標識符即可
方式二與方式一基本相同,只是創建方法是反向的.
方式3 跳轉任意的 ViewController
3-1 實現 Item2 中點擊Button1-3 跳轉 Item1 中的 1-3 界面(即圖1-1中的 箭頭3),拖拽 “Storyboard Reference”控件,選取 StoryBoard為 FristSB,設置標識符.
3-2 進入FristSB.storyboard 選中 1-3所在的控制器,設置 Storyboard ID
簡化之后的Main.storyboard
總結: 若想實現任意跳轉(應注意控制器的壓棧,出棧的關系),只需保證 Main.storyboard “Storyboard Reference”控件的 Referenced ID 與單獨創建storyboard中控制器 的 Storyboard ID 相同即可.