-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
To get started.
- Loading branch information
Showing
1 changed file
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 `[email protected]: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! 😀 |