gpt4 book ai didi

ios - 对于列表详细信息界面 - 数据在详细信息 View 中更新,数据发生更改但不会立即反射(reflect)在详细信息 View 中

转载 作者:行者123 更新时间:2023-11-29 05:14:47 24 4
gpt4 key购买 nike

我正在 Apple Watch 上使用 SwiftUI,并尝试正确使用 @ObservableObject、@ObservedObject 和 @Binding。我正在更新 DetailView 中的值,并且希望将其反射(reflect)在本地,并在全局范围内更改数据。下面的代码有效,但我使用拼凑来强制 DetailView 重新绘制自身:

有更好的方法吗?

-------------- ContentView.swift ---------------

import Combine
import SwiftUI

struct person: Identifiable {
var id:Int = 0
var name:String
init( id: Int, name:String) {
self.id = id
self.name = name
}
}

class AppData: ObservableObject {
@Published var people:[person] = [person(id:0, name:"John"),
person(id:1, name:"Bret"),
person(id:2,name:"Sue"),
person(id:3,name:"Amy")]
}

var gAppData = AppData()

struct ContentView: View {
@ObservedObject var model:AppData
var body: some View {
List( model.people.indices ){ index in
NavigationLink(destination: DetailView(person:self.$model.people[index])) { Text(self.model.people[index].name) }
}
}
}

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

-------------- DetailView.swift ---------------

import SwiftUI

struct DetailView: View {
@Binding var person: person

// Created an unnecessary var to force a redreaw of the view
@State var doRedraw:Bool = true

var body: some View {
VStack(){
Text(person.name)
Button(action:{ self.person.name = "Bob"; self.doRedraw = false }) {
Text("Set Name to Bob")
}
}

}
}

struct DestView_Previews: PreviewProvider {
static var previews: some View {
DetailView(person:.constant(person( id:0, name:"John"))) // what does ".constant" actually do?
}
}

最佳答案

这里的问题是因为只有当您更改 @State@Binding 变量时,您的 View 才会重绘。在这里,您不会更改 Person 变量,而是更改其属性,这不应影响用户界面(因为您没有说要这样做)。我对您的代码进行了一些更改,以展示如何实现此效果,您可以从现在开始继续。您需要记住,到底是什么影响了 UI:

class Person: Identifiable, ObservableObject { // better to assign struct/class names using UpperCamelCase

@Published var name:String // now change of this variable will affect UI
var id:Int = 0

init( id: Int, name:String) {
self.id = id
self.name = name
}

}

// changes in DetailView
struct DetailView: View {

@EnvironmentObject var person: Person

var body: some View {
VStack(){
Text(person.name)
Button(action:{ self.person.name = "Bob" }) {
Text("Set Name to Bob")
}
}

}
}

// preview
struct DetailViewWithoutGlobalVar_Previews: PreviewProvider {
static var previews: some View {
DetailView()
.environmentObject(Person(id: 1, name: "John"))
}
}

更新:列表和详细信息的完整代码

import SwiftUI

class Person: Identifiable, ObservableObject { // better to assign type names using UpperCamelCase

@Published var name: String //{
var id: Int = 0

init( id: Int, name:String) {
self.id = id
self.name = name
}

func changeName(_ newName: String) {
self.name = newName
}

}

class AppData: ObservableObject {
@Published var people: [Person] = [Person(id:0, name:"John"),
Person(id:1, name:"Bret"),
Person(id:2,name:"Sue"),
Person(id:3,name:"Amy")]
}

struct ContentViewWithoutGlobalVar: View {

@EnvironmentObject var model: AppData

var body: some View {
NavigationView { // you forget something to navigate between views
List(model.people.indices) { index in
NavigationLink(destination: DetailView()
.environmentObject(self.model.people[index])) {
PersonRow(person: self.$model.people[index])
}
}
}

}

}

struct PersonRow: View {

@Binding var person: Person // this struct will see changes in Person and show them

var body: some View {
Text(person.name)
}

}

struct DetailView: View {

@EnvironmentObject var person: Person

var body: some View {
VStack(){
Text(self.person.name)
Button(action:{ self.person.changeName("Bob") }) {
Text("Set Name to Bob")
}
}

}
}

struct ContentViewWithoutGlobalVar_Previews: PreviewProvider {
static var previews: some View {
Group {
ContentViewWithoutGlobalVar()
.environmentObject(AppData())

DetailView()
.environmentObject(Person(id: 0, name: "John"))
}
}
}

关于ios - 对于列表详细信息界面 - 数据在详细信息 View 中更新,数据发生更改但不会立即反射(reflect)在详细信息 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59296652/

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