1 、在工程中創建一個storyboard文件,比如我現在"Authorization.storyboard”這個文件。然後我們可以在storyboard可視化視圖中創建多個viewcontroller,navigationcontroller或者Storyboard Reference。
如果storyboard中存在比較多VC,關鍵要找到入口,Storybord Entry Point。
舉例:
在storyboard文件中創建Storyboard Reference,記得在第五個入口設置Storyboard名稱
在Authorization.storyboard中創建Splash ScreenVC Scene,創建步驟如下,SplashScreenVC爲對應的controller文件。注意點,必須填寫Class , Storyboard ID這兩個選項
2、用代碼來獲取剛纔創建的storyboard。也就是第一步的"Authorization.storyboard" 文件。
let storyboard = UIStoryboard(name: "Authorization", bundle: nil)
2-1、storyboard帶有 Storyboard ID方法來獲取 SplashScreenVC。
let current = storyboard.instantiateViewController(withIdentifier: "SplashScreenVC") as! SplashScreenVC
2-2、由閃屏界面切換到登陸輸入框界面(authNavVC)
創建一個RootparentVC:UIViewController,提供一個var current:UIViewController變量。(curent根據不同情況展示不同的vc)
假設一開始是顯示SplashScreenVC(閃屏界面)
init() {
let storyboard = UIStoryboard(name: "Authorization", bundle: nil)
current = storyboard.instantiateViewController(withIdentifier: "SplashScreenVC") as! SplashScreenVC
super.init(nibName: nil, bundle: nil)
}
2-3、在viewdidLoad (當前VC添加新VC,設置新VC的frame,當前view添加新VC的view,新VC move to parentVC)
addChildViewController(current)
current.view.frame = view.bounds
view.addSubview(current.view)
current.didMove(toParentViewController: self)
2-4、切換顯示登陸界面(authNavVC)(當前VC添加新VC,設置新VC的frame,當前view添加新VC的view,新VC move to parentVC)
func showLoginScreen() {
let storyboard = UIStoryboard(name: "Authorization", bundle: nil)
let authNavVC = storyboard.instantiateViewController(withIdentifier: "AuthNavVC") as! UINavigationController
addChildViewController(authNavVC)
authNavVC.view.frame = view.bounds
view.addSubview(authNavVC.view)
authNavVC.didMove(toParentViewController: self)
current.willMove(toParentViewController: nil)
current.view.removeFromSuperview()
current.removeFromParentViewController()
current = authNavVC
}
3、自定義UIStoryboardSegue
import Foundation
class ReplaceSegue : UIStoryboardSegue {
override func perform() {
let sourceViewController = self.source
let destinationViewController = self.destination
let navigationController = sourceViewController.navigationController
navigationController?.pushViewController(destinationViewController, animated: false)
guard var mutableVC = navigationController?.viewControllers else {
debugPrint("[ReplaceSegue] Error: no view controllers")
return
}
guard let sourceViewControllerIndex = mutableVC.index(of: sourceViewController) else {
debugPrint("[ReplaceSegue] Error: no index for source view controller")
return
}
mutableVC.remove(at: sourceViewControllerIndex)
navigationController?.setViewControllers(mutableVC, animated: true)
}
}
3-1、舉例:點擊會話列表某一條信息,攜帶會話ID參數,進入聊天詳情。
在會話列表代碼爲:
performSegue(withIdentifier: "SA_STR_SEGUE_GO_TO_CHAT".localized , sender: dialog.id)
重寫prepare方法。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "SA_STR_SEGUE_GO_TO_CHAT".localized {
if let chatVC = segue.destination as? ChatViewController {
chatVC.dialogID = sender as? String
}
}
}