英文:
<SwiftUI> Passing references between views when using lists with @Binding
问题
我是SwiftUI编程的新手,有一个基本问题。想知道为什么在这个例子中的“selectedIndex”值没有在子视图“TestChildView”中反映出来。我该如何修改以便在子视图中获得正确的“selectedIndex”值?
import SwiftUI
struct TestView: View {
@State var selectedIndex: Int = 0
var body: some View {
NavigationStack {
VStack {
List {
Section(header:Text("Test")) {
ForEach(0 ..< 5, id: \.self) {selectedIndex in
NavigationLink {
TestChildView(selectedIndex: $selectedIndex)
} label: {
Text(String(selectedIndex)
)
}
}
}
}
}
}
}
}
struct TestChildView: View {
@Binding var selectedIndex: Int
var body: some View {
VStack {
Text(String(selectedIndex))
}
}
}
英文:
I'm a newbie in SwiftUI programming and having a basic question.
Wondering why the value in "selectedIndex" in this example is not reflected in the child view, "TestChildView". How can I modify this so that I can get the correct selectedIndex value in the child view?
import SwiftUI
struct TestView: View {
@State var selectedIndex: Int = 0
var body: some View {
NavigationStack {
VStack {
List {
Section(header:Text("Test")) {
ForEach(0 ..< 5, id: \.self) {selectedIndex in
NavigationLink {
TestChildView(selectedIndex: $selectedIndex)
} label: {
Text(String(selectedIndex)
)
}
}
}
}
}
}
}
}
struct TestChildView: View {
@Binding var selectedIndex: Int
var body: some View {
VStack {
Text(String(selectedIndex))
}
}
}
答案1
得分: 1
Currently the @State var selectedIndex ... and the selectedIndex in the ForEach loop are two different things. That is why it is not reflected in the child view, TestChildView.
To ...get the correct selectedIndex value in the child view, remove the
@State var selectedIndex: Int = 0
and the $ in the call of TestChildView and use
TestChildView(selectedIndex: selectedIndex)
Also use let selectedIndex: Int in TestChildView
EDIT-1:
here is the full test code I use to show it works well for me.
import SwiftUI
@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct TestView: View {
var body: some View {
NavigationStack {
VStack {
List {
Section(header:Text("Test")) {
ForEach(0..<5, id: \.self) { selectedIndex in
NavigationLink {
TestChildView(selectedIndex: selectedIndex)
} label: {
Text(String(selectedIndex))
}
}
}
}
}
}
}
}
struct TestChildView: View {
let selectedIndex: Int
var body: some View {
VStack {
Text(String(selectedIndex))
}
}
}
struct ContentView: View {
var body: some View {
TestView()
}
}
英文:
Currently the @State var selectedIndex ... and the selectedIndex in the ForEach loop are two different things. That is why it is not reflected in the child view, TestChildView.
To ...get the correct selectedIndex value in the child view, remove the
@State var selectedIndex: Int = 0
and the $ in the call of TestChildView and use
TestChildView(selectedIndex: selectedIndex)
Also use let selectedIndex: Int in TestChildView
EDIT-1:
here is the full test code I use to show it works well for me.
import SwiftUI
@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct TestView: View {
var body: some View {
NavigationStack {
VStack {
List {
Section(header:Text("Test")) {
ForEach(0..<5, id: \.self) { selectedIndex in
NavigationLink {
TestChildView(selectedIndex: selectedIndex)
} label: {
Text(String(selectedIndex))
}
}
}
}
}
}
}
}
struct TestChildView: View {
let selectedIndex: Int
var body: some View {
VStack {
Text(String(selectedIndex))
}
}
}
struct ContentView: View {
var body: some View {
TestView()
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论