Skip to content

Instantly share code, notes, and snippets.

@yury
Forked from b3ll/BindingObservedObject.swift
Last active November 13, 2019 09:33
Show Gist options
  • Select an option

  • Save yury/0855e4eecf4a8c3d98c9d437ab6c54ef to your computer and use it in GitHub Desktop.

Select an option

Save yury/0855e4eecf4a8c3d98c9d437ab6c54ef to your computer and use it in GitHub Desktop.

Revisions

  1. yury revised this gist Nov 13, 2019. 1 changed file with 9 additions and 14 deletions.
    23 changes: 9 additions & 14 deletions BindingObservedObject.swift
    Original file line number Diff line number Diff line change
    @@ -9,34 +9,29 @@ class Model: ObservableObject {
    @Published var on: Bool = false
    }

    class Thing: NSObject, ObservableObject {

    class Thing: NSObject {
    @ObservedObject var model = Model()

    var objectWillChange: ObservableObjectPublisher {
    model.objectWillChange
    }

    }

    var thing = Thing()

    struct MyView: View {
    struct MyView<T: ObservableObject>: View {

    @EnvironmentObject var thing: Thing // Add dependency for the thing
    @Binding var on: Bool
    @Binding var value: Bool
    @ObservedObject var with: T


    var body: some View {
    Rectangle()
    .foregroundColor(on ? .blue : .red)
    .foregroundColor(value ? .blue : .red)
    .frame(width: 100.0, height: 100.0, alignment: .center)
    .onTapGesture {
    self.on.toggle()
    print(self.on)
    self.value.toggle()
    print(self.value)
    }
    }

    }

    // Present the view controller in the Live View window
    PlaygroundPage.current.liveView = UIHostingController(rootView: MyView(on: thing.$model.on).environmentObject(thing))
    PlaygroundPage.current.liveView = UIHostingController(rootView: MyView(value: thing.$model.on, with: thing.model))
  2. yury revised this gist Nov 13, 2019. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions BindingObservedObject.swift
    Original file line number Diff line number Diff line change
    @@ -2,22 +2,28 @@

    import SwiftUI
    import UIKit
    import Combine
    import PlaygroundSupport

    class Model: ObservableObject {
    @Published var on: Bool = false
    }

    class Thing: NSObject {
    class Thing: NSObject, ObservableObject {

    @ObservedObject var model = Model()

    var objectWillChange: ObservableObjectPublisher {
    model.objectWillChange
    }

    }

    var thing = Thing()

    struct MyView: View {

    @EnvironmentObject var thing: Thing // Add dependency for the thing
    @Binding var on: Bool

    var body: some View {
    @@ -33,4 +39,4 @@ struct MyView: View {
    }

    // Present the view controller in the Live View window
    PlaygroundPage.current.liveView = UIHostingController<MyView>(rootView: MyView(on: thing.$model.on))
    PlaygroundPage.current.liveView = UIHostingController(rootView: MyView(on: thing.$model.on).environmentObject(thing))
  3. Adam Bell created this gist Nov 13, 2019.
    36 changes: 36 additions & 0 deletions BindingObservedObject.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    //: A UIKit based Playground for presenting user interface

    import SwiftUI
    import UIKit
    import PlaygroundSupport

    class Model: ObservableObject {
    @Published var on: Bool = false
    }

    class Thing: NSObject {

    @ObservedObject var model = Model()

    }

    var thing = Thing()

    struct MyView: View {

    @Binding var on: Bool

    var body: some View {
    Rectangle()
    .foregroundColor(on ? .blue : .red)
    .frame(width: 100.0, height: 100.0, alignment: .center)
    .onTapGesture {
    self.on.toggle()
    print(self.on)
    }
    }

    }

    // Present the view controller in the Live View window
    PlaygroundPage.current.liveView = UIHostingController<MyView>(rootView: MyView(on: thing.$model.on))