간단한 연습 앱을 만들어 @EnvironmentObject를 사용하여 데이터를 바인딩하는 방법을 연습하고 있습니다. 앱의 기본보기는 각 셀에 제목이있는 간단한 목록입니다. 셀을 클릭하면 자세히보기가 표시됩니다. 자세히보기에는 기본보기에서 셀의 제목을 변경할 수있는 TextField가 있습니다. textField를 셀 제목과 바인딩하는 방법을 알 수 없습니다.
ForEach
에서 루프를 다음 ContentView
으로 바꿀 수 있습니다 .
// iterate through indices of the `store.items` array
ForEach(0..<store.items.count, id:\.self) { index in
// pass the `index` to the `DetailView`
NavigationLink(destination: DetailView(index: index)) {
Text(self.store.items[index].title)
}
}
그런 다음 index
in을 사용하여 에서 DetailView
바인딩에 액세스합니다 @EnvironmentObject
.
struct DetailView: View {
@EnvironmentObject var store: CPStore
// item index
let index: Int
var body: some View {
VStack {
// now you can access the item binding
TextField("New title", text: $store.items[index].title)
.padding(5)
.frame(height: 50)
.overlay(Rectangle().stroke(Color.gray, lineWidth: 2))
Spacer()
}
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다