SwiftUI 的 ViewController。
核心思想是 ViewController
拥有或至少驱动 View(s),而不是反过来。
博客文章解释了所有内容:SwiftUI 的模型视图控制器
仅是快速入门的基础知识。
ViewController
包,例如通过 git@github.com:ZeeZide/ViewController.git
HomePage.swift
import ViewController
class HomePage: ViewController {
var view: some View {
VStack {
Text("Welcome to MWC!")
.font(.title)
.padding()
Spacer()
}
}
}
ContentView.swift
import ViewController
struct ContentView: View {
var body: some View {
MainViewController(HomePage())
}
}
Settings.swift
import ViewController
class Settings: ViewController {
var view: some View { // the View being controlled
VStack {
Text("Welcome to Settings!")
.font(.title)
.padding()
Spacer()
}
}
}
HomePage
呈现 Settings
的动作import ViewController
class HomePage: ViewController {
func configureApp() {
show(Settings()) // or `present(Settings())`
}
var view: some View {
VStack {
Text("Welcome to MWC!")
.font(.title)
.padding()
Divider()
Button(action: self.configureApp) {
Label("Configure", systemImage: "gear")
}
Spacer()
}
}
}
按下按钮应该以 Sheet 的形式显示设置。
NavigationController
中,在场景视图中import ViewController
struct ContentView: View {
var body: some View {
MainViewController(NavigationController(rootViewController: HomePage()))
}
}
注意,按下按钮会进行导航。类似这样的事情也应该可行
func presentInSheet() {
let vc = SettingsPage()
vc.modalPresentationStyle = .sheet
present(vc)
}
到目前为止,演示利用了一个隐藏的链接。要显式地内联一个 NavigationLink
,请使用包装它的 PushLink
。
PushLink
(直到我得到一个 NavigationLink
init 扩展为止) 以从 HomePage
呈现 Settings
import ViewController
class HomePage: ViewController {
var view: some View {
VStack {
Text("Welcome to MWC!")
.font(.title)
.padding()
Divider()
PushLink("Open Settings", to: Settings())
Spacer()
}
}
}
ViewController 由 ZeeZide 提供。我们喜欢反馈、GitHub 星星、酷炫的合同工作,大概任何你能想到的赞美形式。
想支持我的工作吗? 购买一个应用:Past for iChat、SVG Shaper、Shrugs、HMScriptEditor。你不必使用它! 😀