gpt4 book ai didi

swift - 我如何使用 onDisappear 修饰符来了解 SwiftUI 中被杀死的 View ?

转载 作者:行者123 更新时间:2023-12-04 14:03:19 26 4
gpt4 key购买 nike

我有一个学习项目,你也可以称之为测试/学习项目!其目标是重新创建数据源并尝试使用最新的数据源更新!

在这个项目中,我成功地创建了相同的数据源,而无需向自定义 View 发送或显示数据源!就像下面的这个示例项目,所以我想保持复制的创建的数据源与原始数据源一起更新!

例如:我正在删除原始数据源中的一个元素,我正在尝试读取消失的 View 的 ID!在重复的一个中删除相同的一个!但效果不佳!

目标:正如您在我的项目和代码中看到的那样!我想创建并保持重复的数据源下沉并等于原始数据源,而不将数据源直接发送到自定义 View !甚至发送任何类型的通知!目标是原始数据源应该将零工作让重复的数据源知道它自己,所有工作都属于自定义 View 以找出已删除的项目。

PS:请不要询问用例或类似问题!我正在试验这种方法,看看它是否有效或在哪些地方有用!

struct ContentView: View {

@State private var array: [Int] = Array(0...3)

var body: some View {

VStack(alignment: .leading, spacing: 10.0) {

ForEach(array.indices, id:\.self) { index in

CustomView(id: index) {

HStack {

Text(String(describing: array[index]))
.frame(width: 50.0)

Image(systemName: "trash")
.foregroundColor(.red)
.onTapGesture { array.remove(at: index) }

}

}

}

}
.font(Font.body.bold())

}

}



struct CustomView<Content: View>: View {

let id: Int
let content: () -> Content

@StateObject private var customModel: CustomModel = CustomModel.shared

var body: some View {

content()
.preference(key: IntPreferenceKey.self, value: id)
.onPreferenceChange(IntPreferenceKey.self) { newValue in

if !customModel.array.contains(newValue) { customModel.array.append(newValue) }

}
.onDisappear(perform: {

print("id:", id, "is going get removed!")

customModel.array.remove(at: id)

})

}

}


class CustomModel: ObservableObject {

static let shared: CustomModel = CustomModel()

@Published var array: Array<Int> = Array<Int>() {

didSet {
print(array.sorted())
}
}

}



struct IntPreferenceKey: PreferenceKey {

static var defaultValue: Int { get { return Int() } }

static func reduce(value: inout Int, nextValue: () -> Int) { value = nextValue() }

}

最佳答案

ForEach 依赖数组索引对于这类工作来说是不可靠的,因为您使用的 ID 是 self —— 即 index 的项目。这将导致对 ForEach 中的项目进行不可靠的重新计算,因为它们实际上无法通过索引识别。例如,如果项目 0 被删除,那么项目 1 现在变成项目 0,使得使用索引作为标识符相对无用。

相反,使用实际的唯一 ID 来描述您的模型,一切都按预期工作:


struct Item: Identifiable {
let id = UUID()
var label : Int
}

struct ContentView: View {

@State private var array: [Item] = [.init(label: 0),.init(label: 1),.init(label: 2),.init(label: 3)]

var body: some View {

VStack(alignment: .leading, spacing: 10.0) {

ForEach(array) { item in

CustomView(id: item.id) {

HStack {

Text("\(item.label) - \(item.id)")
.frame(width: 50.0)

Image(systemName: "trash")
.foregroundColor(.red)
.onTapGesture { array.removeAll(where: { $0.id == item.id }) }

}

}

}

}
.font(.body.bold())

}

}



struct CustomView<Content: View>: View {

let id: UUID
let content: () -> Content

@StateObject private var customModel: CustomModel = CustomModel.shared

var body: some View {

content()
.preference(key: UUIDPreferenceKey.self, value: id)
.onPreferenceChange(UUIDPreferenceKey.self) { newValue in

if !customModel.array.contains(where: { $0 == id }) {
customModel.array.append(id)
}

}
.onDisappear(perform: {

print("id:", id, "is going to get removed!")

customModel.array.removeAll(where: { $0 == id })

})

}

}


class CustomModel: ObservableObject {

static let shared: CustomModel = CustomModel()

@Published var array: Array<UUID> = Array<UUID>() {
didSet {
print(array)
}
}

}



struct UUIDPreferenceKey: PreferenceKey {

static var defaultValue: UUID { get { return UUID() } }

static func reduce(value: inout UUID, nextValue: () -> UUID) { value = nextValue() }

}

关于swift - 我如何使用 onDisappear 修饰符来了解 SwiftUI 中被杀死的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69308126/

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