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
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yury/0855e4eecf4a8c3d98c9d437ab6c54ef to your computer and use it in GitHub Desktop.
Save yury/0855e4eecf4a8c3d98c9d437ab6c54ef to your computer and use it in GitHub Desktop.
//: A UIKit based Playground for presenting user interface
import SwiftUI
import UIKit
import Combine
import PlaygroundSupport
class Model: ObservableObject {
@Published var on: Bool = false
}
class Thing: NSObject {
@ObservedObject var model = Model()
}
var thing = Thing()
struct MyView<T: ObservableObject>: View {
@Binding var value: Bool
@ObservedObject var with: T
var body: some View {
Rectangle()
.foregroundColor(value ? .blue : .red)
.frame(width: 100.0, height: 100.0, alignment: .center)
.onTapGesture {
self.value.toggle()
print(self.value)
}
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = UIHostingController(rootView: MyView(value: thing.$model.on, with: thing.model))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment