SwiftUI 从 ForEach 中的按钮打开工作表并传输参数

问题描述

在 SwiftUI Mac 应用程序 (Appkit) 中,我想在单击时打开每个按钮的 IndexDialog 表。
我想把对应的索引传给IndexDialog。
无论按下哪个按钮,索引 1 都会被传送。
如何正确地做到这一点?

struct TEST_Dialog: View {
  @State var showDialog = false
  
  var body: some View {
    vstack{
      ForEach(1...5,id: \.self) { index in

        Button("\(index)"){
          showDialog = true
        }
        .sheet(isPresented: $showDialog) {
          Print("index \(index)")
          IndexDialog(isPresented: $showDialog,index:  index)
        }
      }
    }
  }
}

struct IndexDialog : View {
  @Binding var isPresented : Bool
  var index : Int

  var body: some View {

    vstack {
      Text("INDEX").font(.headline)
      Text(">\(index)<").font(.callout)

      Button("Close") {
        isPresented = false
      }
    }.padding()
  }
}


解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)