1

抱歉,如果我的问题很愚蠢,我是编程的初学者。我有一个从我的视图模型数组生成的列表中指向详细视图的导航链接。在详细视图中,我希望能够改变点击元素的属性之一,但我似乎无法弄清楚如何做到这一点。我不认为我解释得很好,所以这里是代码。

// model
struct Activity: Identifiable {
    var id = UUID()
    var name: String
    var completeDescription: String
    var completions: Int = 0
}

// view model
class ActivityViewModel: ObservableObject {
    @Published var activities: [Activity] = []
}

// view
struct ActivityView: View {
    @StateObject var viewModel = ActivityViewModel()
    @State private var showingAddEditActivityView = false
    var body: some View {
        NavigationView {
            VStack {
                List {
                    ForEach(viewModel.activities, id: \.id) {
                        activity in
                        NavigationLink(destination: ActivityDetailView(activity: activity, viewModel: self.viewModel)) {
                            HStack {
                                VStack {
                                    Text(activity.name)
                                    Text(activity.miniDescription)
                                }
                                Text("\(activity.completions)")
                            }
                        }
                    }
                }
            }
            .navigationBarItems(trailing: Button("Add new"){
                self.showingAddEditActivityView.toggle()
            })
            .navigationTitle(Text("Activity List"))
        }
        .sheet(isPresented: $showingAddEditActivityView) {
            AddEditActivityView(copyViewModel: self.viewModel)
        }
    }
}

// detail view
struct ActivityDetailView: View {
    @State var activity: Activity
    @ObservedObject var viewModel: ActivityViewModel
    
    var body: some View {
        VStack {
            Text("Number of times completed: \(activity.completions)")
            Button("Increment completion count"){
                activity.completions += 1
                updateCompletionCount()
            }
            Text("\(activity.completeDescription)")
        }
    }
    func updateCompletionCount() {
         var tempActivity = viewModel.activities.first{ activity in activity.id == self.activity.id
         }!
        tempActivity.completions += 1
    }
}

// Add new activity view (doesn't have anything to do with question)

struct AddEditActivityView: View {
    @ObservedObject var copyViewModel : ActivityViewModel
    @State private var activityName: String = ""
    @State private var description: String = ""
    var body: some View {
        VStack {
            TextField("Enter an activity", text: $activityName)
            TextField("Enter an activity description", text: $description)
            Button("Save"){
                // I want this to be outside of my view
                saveActivity()
            }
        }
    }
    func saveActivity() {
        copyViewModel.activities.append(Activity(name: self.activityName, completeDescription: self.description))
        print(copyViewModel.activities)
    }
}

在详细视图中,我正在尝试更新该特定活动的完成计数,并让它更新我的视图模型。我上面尝试的方法可能没有意义,显然不起作用。我只是把它留下来展示我的尝试。

感谢您提供任何帮助或见解。

4

2 回答 2

2

问题在这里:

struct ActivityDetailView: View {
    @State var activity: Activity
    ...

这需要是@Binding为了使更改反映在父视图中。也没有必要传递整个viewModelin - 一旦你有了@Binding,你就可以摆脱它。

// detail view
struct ActivityDetailView: View {
    @Binding var activity: Activity /// here!
    
    var body: some View {
        VStack {
            Text("Number of times completed: \(activity.completions)")
            Button("Increment completion count"){
                activity.completions += 1
            }
            Text("\(activity.completeDescription)")
        }
    }
}

但是你如何获得绑定?如果您使用的是 iOS 15,则可以直接循环$viewModel.activities

/// here!
ForEach($viewModel.activities, id: \.id) { $activity in
    NavigationLink(destination: ActivityDetailView(activity: $activity)) {
        HStack {
            VStack {
                Text(activity.name)
                Text(activity.miniDescription)
            }
            Text("\(activity.completions)")
        }
    }
}

而对于 iOS 14 或更低版本,您需要循环遍历索引。但它有效。

/// from https://stackoverflow.com/a/66944424/14351818
ForEach(Array(zip(viewModel.activities.indices, viewModel.activities)), id: \.1.id) { (index, activity)  in
    NavigationLink(destination: ActivityDetailView(activity: $viewModel.activities[index])) {
        HStack {
            VStack {
                Text(activity.name)
                Text(activity.miniDescription)
            }
            Text("\(activity.completions)")
        }
    }
}
于 2021-09-10T18:56:47.983 回答
1

您正在更改并增加 tempActivity 的值,因此它不会影响主数组或数据源。

您可以在视图模型中添加一个更新函数并从视图中调用。

视图模型负责此更新。

class ActivityViewModel: ObservableObject {
    @Published var activities: [Activity] = []
    
    func updateCompletionCount(for id: UUID) {
        if let index = activities.firstIndex(where: {$0.id == id}) {
            self.activities[index].completions += 1
        }
    }
}
struct ActivityDetailView: View {
    var activity: Activity
    var viewModel: ActivityViewModel
    
    var body: some View {
        VStack {
            Text("Number of times completed: \(activity.completions)")
            Button("Increment completion count"){
                updateCompletionCount()
            }
            Text("\(activity.completeDescription)")
        }
    }
    func updateCompletionCount() {
        self.viewModel.updateCompletionCount(for: activity.id)
    }
}

如果没有进一步的操作,则不需要 @State 或 @ObservedObject 来查看详细信息。

于 2021-09-10T18:55:55.687 回答