Created
August 8, 2019 12:50
-
-
Save PaulWoodIII/190afccfea4aa597934ca7422315bb6c to your computer and use it in GitHub Desktop.
There is more than one way to handle Bindings in your code, here's two ways I know how
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import PlaygroundSupport | |
struct BoolHolder: View { | |
@State var boolProperty: Bool = false | |
var body: some View { | |
VStack{ | |
if boolProperty { | |
Text("The Bool Value is True") | |
.font(.footnote) | |
.foregroundColor(Color.black) | |
} else { | |
Text("The Bool Value is False") | |
.font(.footnote) | |
.foregroundColor(Color.black.opacity(0.6)) | |
} | |
BoolTogglerPropertyWrapper(toggleState: $boolProperty) | |
BoolTogglerVanilla(toggleState: $boolProperty) | |
} | |
} | |
} | |
struct BoolTogglerPropertyWrapper: View { | |
@Binding var toggleState: Bool | |
var body: some View { | |
Toggle(isOn: $toggleState, label: { | |
Text("Toggle Me Here") | |
}).padding() | |
} | |
} | |
struct BoolTogglerVanilla: View { | |
var toggleState: Binding<Bool> | |
var body: some View { | |
Toggle(isOn: toggleState, label: { | |
Text("Toggle Me Here") | |
}).padding() | |
} | |
} | |
let viewController = UIHostingController(rootView: BoolHolder()) | |
PlaygroundPage.current.liveView = viewController |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment