Requested
Seen
23 occasions
Swift newbie right here. I’m attempting to create a reusable element that merely renders both a stuffed or empty systemImage circle primarily based on a price saved in state. I’ve put collectively a replica instance beneath. Proper now it solely seems to be updating the worth in state (in formData
), however the UI would not appear to be updating (the circle is just not filling up). I’ve a sense the Picture
and Textual content
elements are set as much as react to state modifications, however I am unsure methods to repair from there. Thanks prematurely!
import SwiftUI
struct Check: View {
@ObservedObject var formData = FormData()
var physique: some View {
VStack {
Textual content("Efficiency: (String(formData.targets.efficiency))")
Toggle(isOn: Binding(
get: { formData.targets.efficiency },
set: { formData.targets.efficiency = $0 }
)) {
Textual content("Efficiency")
}
ChildWrapper(isPerformanceEnabled: $formData.targets.efficiency)
}
}
}
struct ChildWrapper: View {
@Binding var isPerformanceEnabled: Bool
var physique: some View {
VStack {
Picture(systemName: isPerformanceEnabled ? "circle.fill" : "circle")
.font(.system(dimension: 50))
.foregroundColor(isPerformanceEnabled ? .inexperienced : .crimson)
.onTapGesture {
isPerformanceEnabled.toggle()
}
Textual content(isPerformanceEnabled ? "Enabled" : "Disabled")
}
}
}
struct Test_Previews: PreviewProvider {
static var previews: some View {
Check()
}
}
3
Should you use this:
import SwiftUI
struct Check: View {
// @ObservedObject var formData = FormData()
@State non-public var isPerformanceEnabled = false
var physique: some View {
VStack {
Textual content("Efficiency: (String(isPerformanceEnabled))")
Toggle(isOn: $isPerformanceEnabled) {
Textual content("Efficiency")
}
ChildWrapper(isPerformanceEnabled: //$formData.targets.efficiency)
$isPerformanceEnabled)
}
}
}
struct ChildWrapper: View {
@Binding var isPerformanceEnabled: Bool
var physique: some View {
VStack {
Picture(systemName: isPerformanceEnabled ? "circle.fill" : "circle")
.font(.system(dimension: 50))
.foregroundColor(isPerformanceEnabled ? .inexperienced : .crimson)
.onTapGesture {
isPerformanceEnabled.toggle()
}
Textual content(isPerformanceEnabled ? "Enabled" : "Disabled")
}
}
}
struct Test_Previews: PreviewProvider {
static var previews: some View {
Check()
}
}
possibly work however is just not the whole resolution for you. I believe you do examine the right binding $ of the ObservedObject.
lang-swift