From f656c49f18a31e6b893fcced2c748cfa47717633 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Helge=20He=C3=9F?= Date: Tue, 26 Apr 2022 18:35:37 +0200 Subject: [PATCH] Add a very basic explanation To get started. --- README.md | 174 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) diff --git a/README.md b/README.md index 788032d..08411c7 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,177 @@ ViewController's for SwiftUI. WIP. + +The core idea is that the `ViewController` is owning, or at least driving, +the View(s). Not the other way around. + + +## How to Use + +More details will be posted but to get started. + +### Step A: Setup Project and Root VC + +- create a SwiftUI project in Xcode (iOS is tested better) +- add the `ViewController` package, + e.g. via `git@github.com:ZeeZide/ViewController.git` +- create a new RootViewController, e.g. `HomePage.swift`: + ```swift + import ViewController + + class HomePage: ViewController { + + struct ContentView: View { + + var body: some View { + VStack { + Text("Welcome to MWC!") + .font(.title) + .padding() + + Spacer() + } + } + } + } + ``` +- Instantiate that in the scene view, the `ContentView.swift` + generated by Xcode: + ```swift + import ViewController + + struct ContentView: View { + var body: some View { + MainViewController(HomePage()) + } + } + ``` +- Compile and Run, should show the HomePage + +### Step B: Add a presented VC and navigate to it + +- create a new ViewController, e.g. `Settings.swift`: + ```swift + import ViewController + + class Settings: ViewController { + + struct ContentView: View { + + var body: some View { + VStack { + Text("Welcome to Settings!") + .font(.title) + .padding() + + Spacer() + } + } + } + } + ``` +- Add an action to present the `Settings` from the `HomePage`: + ```swift + import ViewController + + class HomePage: ViewController { + + func configureApp() { + show(Settings()) // or `present(Settings())` + } + + struct ContentView: View { + + @EnvironmentObject private var viewController : HomePage + + var body: some View { + VStack { + Text("Welcome to MWC!") + .font(.title) + .padding() + + Divider() + + Button(action: viewController.configureApp) { + Label("Configure", systemImage: "gear") + } + + Spacer() + } + } + } + } + ``` + +Pressing the button should show the settings in a sheet. + + +### Step C: Add a NavigationController for Navigation :-) + +- Wrap the HomePage in a `NavigationController`, in the scene view: + ```swift + 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: +```swift +func presentInSheet() { + let vc = SettingsPage() + vc.modalPresentationStyle = .sheet + present(vc) +} +``` + + +### Adding a `PushLink` + +The presentations so far make use of a hidden link. To explicitly +inline a `NavigationLink`, use `PushLink`, which wraps that. + +- Add a `PushLink` (until I get an `NavigationLink` init extension working) + to present the `Settings` from the `HomePage`: + ```swift + import ViewController + + class HomePage: ViewController { + + struct ContentView: View { + + var body: some View { + VStack { + Text("Welcome to MWC!") + .font(.title) + .padding() + + Divider() + + PushLink("Open Settings", to: Settings()) + + Spacer() + } + } + } + } + ``` + + +### Who + +ViewController is brought to you by [ZeeZide](https://zeezide.de). +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](https://apps.apple.com/us/app/past-for-ichat/id1554897185), +[SVG Shaper](https://apps.apple.com/us/app/svg-shaper-for-swiftui/id1566140414), +[Shrugs](https://shrugs.app/), +[HMScriptEditor](https://apps.apple.com/us/app/hmscripteditor/id1483239744). +You don't have to use it! 😀