根據條件加載不同的 storyboard
項目啟動時答朋,根據條件加載不同的 storyboard,或加載 storyboard 中的不同 ViewController
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
var vc: UIViewController!
if true {
vc = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
} else {
vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Storyboard ID")
}
window?.rootViewController = vc
window?.makeKeyAndVisible()
return true
}
用 instantiateInitialViewController
方法癌佩,就會進 storyboard 中初始箭頭所指的界面。
Is Initial View Controller
注意 withIdentifier 后跟的是 viewController 的 storyboard ID 而不是類名。
多個 Storyboard
storyboard 里界面多了后戈鲁,操作起來會不太方便丧鸯,可以使用多個 storyboard蛤铜,進行模塊化。
按住 command 選中要提到第二個 storyboard 的 viewController,Xcode 頂部菜單 Editor->Refactor to Storyboard...
頂部菜單
彈出框中輸入 storyboard 名字
新的.storyboard 文件
在原.storyboard 文件中出現(xiàn)一個 Storyboard Reference围肥,雙擊跳到新文件剿干,可看到剛才選中的 View Controller 都在新 .storyboard 文件中。
Storyboard Reference
也可以手動添加 Storyboard Reference穆刻,手動創(chuàng)建.storyboard 文件怨愤,在 Storyboard Reference 的屬性中設置文件名,就關聯(lián)起來了蛹批。
手動添加 Storyboard Reference
關聯(lián)文件名
使用 storyboard 時頁面?zhèn)髦?/h2>
注意設置 segue 的 id
segue 的 id
class MainController: UIViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.identifier {
case "segueid":
let vc = segue.destination as! MainController
vc.x = 99 // 傳值
default:
break
}
}
}