我试图在我的SwiftUI应用中将数据(incomeAmount)从第一个视图传递到第二个视图,但是我需要声明BindingString。我需要声明BindingString是什么意思?
查看1
struct ContentView: View {
@State var incomeAmount = ""
var body: some View {
NavigationView {
VStack {
TextField("Your income amount", text: $incomeAmount)
.frame(width: 300)
.padding(.bottom, 30)
NavigationLink(destination: NewView()){
Text("Continue")
.frame(width: 300, height: 50, alignment: .center)
.background(Color.black)
.cornerRadius(130)
.foregroundColor(.white)
.padding(.bottom, 30)
}
Text("$\(incomeAmount)")
}.navigationTitle("First View")
}
}
}
查看2
struct NewView: View {
@Binding var incomeAmount: String
var body: some View {
NavigationView {
VStack {
Text("$\(incomeAmount)")
}
}.navigationTitle("Second View")
}
}
struct NewView_Previews: PreviewProvider {
static var previews: some View {
NewView(incomeAmount: <#Binding<String>#>)
}
}
答案 0 :(得分:0)
要将收入金额传递到第二个视图,您必须执行以下操作:
NavigationLink(destination: NewView(incomeAmount: $incomeAmount)) { ... }
此外,您需要将其提供给:
struct NewView_Previews: PreviewProvider {
static var previews: some View {
NewView(incomeAmount: .constant("something here"))
}
}
由于在PreviewProvider中,NewView希望传入一个Binding