1

I try to build an app in MacOS SwiftUI, where two different subviews of main contentView shows different parts of Axis element:

struct ContentView: View {
    @Binding var document: GlyphDesignerDocument
    
    var body: some View {
        HStack {
            #warning("if AxesSlidersView is not commented, and Axis will be deleted — program explodes")
            AxesSlidersView(axes: $document.axes)
            AxesView(axes: $document.axes,
                     addRows: {document.axes.insert(Axis("z", bounds: 0...1000), at: $0)},
                     removeRows: {document.axes.remove(at: $0)},
                     addAxis: {document.axes.append(Axis("z", bounds: 0...1000))})     
        }
    }

Subviews works great, everything updates in both ways, but application hangs-up when AxisView will delete one Axis from axes array.

All code is available at https://github.com/typoland/GlyphDesignerTest AxesView looks like this:

struct AxesView : View {
    @Binding var axes: [Axis]
    @State var selected: Int? = nil
    var addRows: (_ at:Int) -> Void
    var removeRows: (_ at: Int) -> Void
    var addAxis: () -> Void
    var body: some View {
        VStack {
            .... Shows Axes
        }
    }
}

struct AxisView: View {
    
    @Binding var axis: Axis
    var insert: () -> Void
    var delete: () -> Void
    @Binding var selected: Bool
    
    var body: some View {
        HStack {
            .... Shows one Axis
        }
    }
}
struct AxesSlidersView: View {
    @Binding var axes: [Axis]
    var body: some View {
        VStack {
            .... Shows Axes
        }
    }
}

Is it SwiftUI problem? How to deal with this?

Github version, whole small app looks like this:

Screenshot of app, full code at gitHub

1 Answer 1

2

I recall there were problems with direct binding with onDelete. Try with proxy binding in AxesSlidersView as below:

Slider(value: Binding(get: { axes[index].at }, set: {axes[index].at = $0}), in: axes[index].bounds)
    .frame(idealWidth: metrics.frame(in: .global).width*0.6)
Sign up to request clarification or add additional context in comments.

3 Comments

I know, this is not place to say „Thank You” but „Thank You”. At least one week I tried to solve this problem.
But now changes made on .at in SlidersView are not reflected in AxesView...
When I made proxy in upper view Binding(get: {axes[index]}, set:{axes[index] = $0}) it started to work as should. Could you edit your answer, please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.