Swiftpack.co - ZeeZide/ViewController as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by ZeeZide.
ZeeZide/ViewController 0.3.1
MVC for SwiftUI (yes, seriously)
⭐️ 61
🕓 2 weeks ago
iOS macOS
.package(url: "https://github.com/ZeeZide/ViewController.git", from: "0.3.1")

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! 😀

GitHub

link
Stars: 61
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

Dismissed!
2 weeks 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