ViewController's for SwiftUI.
The core idea is that the ViewController
is owning, or at least driving,
the View(s). Not the other way around.
Blog entry explaining all the things: Model View Controller for SwiftUI
Just the basics to get started quickly.
ViewController
package,
e.g. via [email protected]: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
generated by Xcode:
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()
}
}
}
Settings
from the HomePage
:
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()
}
}
}
Pressing the button should show the settings in a sheet.
NavigationController
, in the scene view:
import ViewController
struct ContentView: View {
var body: some View {
MainViewController(NavigationController(rootViewController: HomePage()))
}
}
Note pressing the button does a navigation. Things like this should also work:
func presentInSheet() {
let vc = SettingsPage()
vc.modalPresentationStyle = .sheet
present(vc)
}
PushLink
The presentations so far make use of a hidden link. To explicitly
inline a NavigationLink
, use PushLink
, which wraps that.
PushLink
(until I get an NavigationLink
init extension working)
to present the Settings
from the HomePage
:
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 is brought to you by ZeeZide. We like feedback, GitHub stars, cool contract work, presumably any form of praise you can think of.
Want to support my work? Buy an app: Past for iChat, SVG Shaper, Shrugs, HMScriptEditor. You don't have to use it! 😀
link |
Stars: 82 |
Last commit: 1 year ago |
This minor release fixes a race when dismissing a sheet. When doing so, the ContentView would sometimes show an error while the sheet contents refresh during slide-out.
Also allows setting the log level using the LOGLEVEL
environment variable (error/debug are common values).
Plus a few fixes in the debug panel.
Finally the NavigationController
now supports a style, so that navigation can be forced to use a stack on iOS (often appropriate in sheets on iPad).
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics