gpt4 book ai didi

arrays - 在 SwiftUI 中更新/编辑数组元素的最佳方式

转载 作者:行者123 更新时间:2023-12-03 15:54:39 28 4
gpt4 key购买 nike

我有一系列可识别的 Training 元素。每个 Training 元素只有两个属性,name 和 isRequired。

将数组元素的现有值更新为新的编辑值的最直接(Swiftyiest)方法是什么......编辑后的值稍后将提交到数据库。

是否可以在 EditTraining 中设置状态 View (Child)传入(Parent)值然后在 subview 中编辑状态?

我被困在这个问题上的时间比我承认的要长。

非常感谢您的任何帮助!

这是代码:

import SwiftUI

struct Training: Identifiable {
let id: String
let trainingName: String
let isRequired: Bool
}

class GetTrainings: ObservableObject {
@Published var items = [Training]()

init() {
self.items = [
Training(id: "ttt1", trainingName: "Safety", isRequired: true),
Training(id: "ttt2", trainingName: "Administrative", isRequired: false),
Training(id: "ttt3", trainingName: "Computer", isRequired: true),
Training(id: "ttt4", trainingName: "People", isRequired: true),
Training(id: "ttt5", trainingName: "Managerial", isRequired: true),
]
}
}

struct TrainingList: View {

@ObservedObject var trainings = GetTrainings()

var body: some View {
NavigationView {
VStack {
List {

ForEach(trainings.items) { training in

HStack {
NavigationLink(destination: TrainingView(training: training)) {
Text("\(training.trainingName)")
}
}
}

}
}.navigationBarTitle("Training List")
}
}
}

struct TrainingView: View {

var training: Training

var body: some View {

VStack {

Text("\(training.trainingName)").font(.body)
Text("\(training.isRequired == true ? "Required Training" : "Training Not Required")")

HStack {
NavigationLink(destination: EditTraining(training: training)) {
Text("Edit Training Details")
}
}
}.navigationBarTitle("\(training.trainingName) Page", displayMode: .inline)

}
}

struct EditTraining: View {

var training: Training

// Can I set the state values to the passed in values ???
@State private var newName: String = ""
@State private var isRequiredTraining: Bool = false
//@Binding var name: String = training.trainingName ????

private func submitData() {

let newName = self.newName
let newBoolVal = self.isRequiredTraining

print("Firebase Sync Id is :\(training.id) Text: \(newName) and Bool: \(newBoolVal)")

}

var body: some View {
VStack {
Form {
Section (header: Text("Edit")) {

Text("\(training.trainingName)")
/* TextField should Populate With passed In Training Name Here*/
TextField("New Name", text: self.$newName)
Toggle(isOn: self.$isRequiredTraining) {
Text("Is Required")
}
}

Section {

Button(action: {
self.submitData()
}) {
Text("Submit")
}

}
}
}.navigationBarTitle("Edit Training Page", displayMode: .inline)
}
}

struct ContentView: View {

var body: some View {

VStack {

TrainingList()

}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

最佳答案

在这种情况下,我认为对模型使用 ObservableObject 更可取,因为它允许通过引用模型对象深入层次结构并在工作流程中保持最新状态。

这是一个解决方案。使用 Xcode 11.4/iOS 13.4 测试

demo

class Training: ObservableObject, Identifiable {
let id: String
@Published var trainingName: String
@Published var isRequired: Bool

init(id: String, trainingName: String, isRequired: Bool) {
self.id = id
self.trainingName = trainingName
self.isRequired = isRequired
}
}

class GetTrainings: ObservableObject {
@Published var items = [Training]()

init() {
self.items = [
Training(id: "ttt1", trainingName: "Safety", isRequired: true),
Training(id: "ttt2", trainingName: "Administrative", isRequired: false),
Training(id: "ttt3", trainingName: "Computer", isRequired: true),
Training(id: "ttt4", trainingName: "People", isRequired: true),
Training(id: "ttt5", trainingName: "Managerial", isRequired: true),
]
}
}

struct TrainingList: View {

@ObservedObject var trainings = GetTrainings()

var body: some View {
NavigationView {
VStack {
List {

ForEach(trainings.items) { training in

HStack {
NavigationLink(destination: TrainingView(training: training)) {
Text("\(training.trainingName)")
}
}
}

}
}.navigationBarTitle("Training List")
.onAppear {
self.trainings.objectWillChange.send() // refresh
}
}
}
}

struct TrainingView: View {

@ObservedObject var training: Training

var body: some View {

VStack {

Text("\(training.trainingName)").font(.body)
Text("\(training.isRequired == true ? "Required Training" : "Training Not Required")")

HStack {
NavigationLink(destination: EditTraining(training: training)) {
Text("Edit Training Details")
}
}
}.navigationBarTitle("\(training.trainingName) Page", displayMode: .inline)

}
}

struct EditTraining: View {

@ObservedObject var training: Training

@State private var newName: String
@State private var isRequiredTraining: Bool

init(training: Training) {
self.training = training
self._newName = State(initialValue: training.trainingName)
self._isRequiredTraining = State(initialValue: training.isRequired)
}

private func submitData() {

let newName = self.newName
let newBoolVal = self.isRequiredTraining

print("Firebase Sync Id is :\(training.id) Text: \(newName) and Bool: \(newBoolVal)")

self.training.trainingName = newName
self.training.isRequired = newBoolVal
}

var body: some View {
VStack {
Form {
Section (header: Text("Edit")) {

Text("\(training.trainingName)")
/* TextField should Populate With passed In Training Name Here*/
TextField("New Name", text: self.$newName)
Toggle(isOn: self.$isRequiredTraining) {
Text("Is Required")
}
}

Section {

Button(action: {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder),
to:nil, from:nil, for:nil)
self.submitData()
}) {
Text("Submit")
}

}
}
}.navigationBarTitle("Edit Training Page", displayMode: .inline)
}
}

关于arrays - 在 SwiftUI 中更新/编辑数组元素的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62106227/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com