Skip to content

Commit

Permalink
Add a very basic explanation
Browse files Browse the repository at this point in the history
To get started.
  • Loading branch information
helje5 committed Apr 26, 2022
1 parent 1d80032 commit f656c49
Showing 1 changed file with 174 additions and 0 deletions.
174 changes: 174 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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! 😀

0 comments on commit f656c49

Please sign in to comment.