删除对象时,同一数组绑定到两个SwiftUI Views挂断应用程序 [英] Same array binded to two SwiftUI Views hangs-up App when object deleted

查看:40
本文介绍了删除对象时,同一数组绑定到两个SwiftUI Views挂断应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在MacOS SwiftUI中构建一个应用程序,其中主contentView的两个不同子视图显示了 Axis 元素的不同部分:

  struct ContentView:查看{@Binding var文档:GlyphDesignerDocumentvar body:some View {HStack {#warning(如果未对AxesSlidersView进行注释,则将删除Axis-程序爆炸")AxesSlidersView(轴:$ document.axes)AxesView(axes:$ document.axes,addRows:{document.axes.insert(Axis("z",界限:0 ... 1000),at:$ 0)},removeRows:{document.axes.remove(at:$ 0)},addAxis:{document.axes.append(Axis("z",bounds:0 ... 1000))})}} 

子视图很好用,两种方式都可以更新,但是当 AxisView axes 数组中删除一个 Axis 时,应用程序将挂起.

所有代码都可以在

解决方案

我还记得 onDelete 的直接绑定存在问题.尝试在 AxesSlidersView 中使用代理绑定,如下所示:

  Slider(值:Binding(获取:{axes [index] .at},设置:{axes [index] .at = $ 0}),在以下位置:axes [index] .bounds).frame(idealWidth:metrics.frame(in:.global).width * 0.6) 

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:

解决方案

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)

这篇关于删除对象时,同一数组绑定到两个SwiftUI Views挂断应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆