ViewController

0.3.1

MVC for SwiftUI (yes, seriously)
ZeeZide/ViewController

What's New

Dismissed!

2022-05-01T14:25:48Z

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).

ViewController

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

Quick: How to Use

Just the basics to get started quickly.

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:
    import ViewController
    
    class HomePage: ViewController {
      
      var view: some View {
        VStack {
          Text("Welcome to MWC!")
            .font(.title)
            .padding()
          
          Spacer()
        }
      }
    }
  • Instantiate that in the scene view, the ContentView.swift generated by Xcode:
    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:
    import ViewController
    
    class Settings: ViewController {
      
      var view: some View { // the View being controlled
        VStack {
          Text("Welcome to Settings!")
            .font(.title)
            .padding()
          
          Spacer()
        }
      }
    }
  • Add an action to present the 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.

Step C: Add a NavigationController for Navigation :-)

  • Wrap the HomePage in a 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)
}

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:
    import ViewController
    
    class HomePage: ViewController {
      
      var view: 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. 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! 😀

Description

  • Swift Tools 5.5.0
View More Packages from this Author

Dependencies

  • None
Last updated: Fri Jan 20 2023 20:51:34 GMT-0500 (GMT-05:00)