带有绑定变量的SwiftUI演示表在首次显示时不起作用 [英] SwiftUI presenting sheet with Binding variable doesn't work when first presented

查看:33
本文介绍了带有绑定变量的SwiftUI演示表在首次显示时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在工作表中显示一个带有@Binding字符串变量的视图,该变量仅在TextField中显示/绑定此变量。

在我的主ContentView中,我有一个字符串数组,我使用ForEach循环遍历该数组的索引来显示该数组,其中每个按钮都包含循环元素的文本。

按钮操作很简单:将@State";index";-变量设置为按下按钮的element-index并显示工作表。

这是我的内容视图:

struct ContentView: View {
    
    @State var array = ["first", "second", "third"]
    @State var showIndex = 0
    @State var showSheet = false
    
    var body: some View {
        VStack {
            ForEach (0 ..< array.count, id:.self) { i in
                Button("(array[i])") {
                    showIndex = i
                    showSheet = true
                }
            }
            // Text("(showIndex)") // if I uncomment this line, it works!
        }
        .sheet(isPresented: $showSheet, content: {
            SheetView(text: $array[showIndex])
        })
        .padding()
    }
}

这里是SheetView:

struct SheetView: View {
    @Binding var text: String
    @Environment(.presentationMode) var presentationMode
    
    var body: some View {
        VStack {
            TextField("text:", text: $text)
            Button("dismiss") {
                presentationMode.wrappedValue.dismiss()
            }
        }.padding()
    }
}
问题是,当我第一次打开应用程序并按下";Second";按钮时,工作表打开并在TextField中显示";first";。然后,我可以取消该工作表,并再次按下第二个&q;按钮,结果相同。

如果我随后按下";第三";或";First";按钮,则从那时起一切正常。按任何按钮都会产生正确的行为。

Preview

有趣的是,如果我取消注释显示showIndex变量的文本所在的行,它将从第一次开始工作。

这是错误,还是我做错了什么?

推荐答案

您应该使用自定义绑定、自定义结构来解决这个问题,这是一个复杂的问题。参见示例:

struct ContentView: View {
    
    @State private var array: [String] = ["first", "second", "third"]
    @State private var customStruct: CustomStruct?
    
    
    var body: some View {
        VStack {
            
            ForEach (array.indices, id:.self) { index in
                
                Button(action: { customStruct = CustomStruct(int: index) }, label: {
                    Text(array[index]).frame(width: 100)
                    
                })
                
            }
            
        }
        .frame(width: 300, height: 300, alignment: .center)
        .background(Color.gray.opacity(0.5))
        .sheet(item: $customStruct, content: { item in SheetView(text: Binding.init(get: { () -> String in return array[item.int] },
                                                                                    set: { (newValue) in array[item.int] = newValue }) ) })
    }
}



struct CustomStruct: Identifiable {
    let id: UUID = UUID()
    var int: Int
}



struct SheetView: View {
    @Binding var text: String
    @Environment(.presentationMode) var presentationMode
    
    var body: some View {
        VStack {
            TextField("text:", text: $text)
            Button("dismiss") {
                presentationMode.wrappedValue.dismiss()
            }
        }.padding()
    }
}

这篇关于带有绑定变量的SwiftUI演示表在首次显示时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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