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
}
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
}
}
}