gpt4 book ai didi

ios - 关闭自定义模式/覆盖不更新 SwiftUI 中的 View 内容

转载 作者:行者123 更新时间:2023-12-01 19:31:13 26 4
gpt4 key购买 nike

更新:我对其进行了一些重构以使用 @ObservableObject ,但问题依然存在
在我的 SwiftUI 应用程序中,我有一个 LazyVGrid填充有 n 张卡片和一个新事件按钮。当我按下新事件按钮时,我有一个覆盖 View 的自定义模式,让用户添加一个新事件。然而,在关闭 View 后(通过点击 Create Event 按钮),模式消失了,但是新的事件卡不会添加到 LazyVGrid .只有在我强制退出应用程序并重新打开它时才会出现。
这是主要的ContentView :

struct ContentView: View {
@State var progress: Double = 0.0
@State var editMode: Bool = false
@State var angle: Double = 0
@State var showModal: Bool = false

@ObservedObject var model: Model

var columns: [GridItem] = Array(repeating: GridItem(.flexible(), spacing: 10), count: 2)

func animate(while condition: Bool) {
self.angle = condition ? -0.6 : 0

withAnimation(Animation.linear(duration: 0.125).repeatForever(while: condition)) {
self.angle = 0.6
}

if !condition {
self.angle = 0
}
}

var body: some View {
ZStack {
NavigationView {
LazyVGrid(columns: columns, spacing: 10) {
ForEach(model.events, id: \.self) { event in
SmallCardView(event: event, angle: $angle)
}

if !showModal {
AddEventButtonView(
editMode: $editMode,
showModal: $showModal,
namespace: namespace
)
} else {
Spacer().frame(height: 100)
}
}
.onLongPressGesture {
if !self.editMode {
self.editMode = true
animate(while: self.editMode)
}
}
}

if self.showModal {
AddEventView(namespace: namespace, events: self.$model.events, showModal: $showModal)
}

}
}
}
这是添加事件按钮的 View :
struct AddEventButtonView: View {
@Binding var editMode: Bool
@Binding var showModal: Bool

var namespace: Namespace.ID

var body: some View {
Image(systemName: "calendar.badge.plus")
.frame(height: 100)
.frame(maxWidth: .infinity)
.opacity(self.editMode ? 0.0 : 1.0)
.onTapGesture {
withAnimation {
self.showModal = true
}
}
}
}
这是我的模态视图:
struct AddEventView: View {
@State var eventName: String = ""
@State var endDate = Date()
@State var gradientIndex: Int = 0

@Binding var showModal: Bool
@Binding var events: [Event]

var namespace: Namespace.ID

init(namespace: Namespace.ID, events: Binding<[Event]>, showModal: Binding<Bool>) {
self.namespace = namespace
self._events = events
self._showModal = showModal
}

var body: some View {
VStack(spacing: 30.0) {
//...

Button(action: {
let event = Event(name: eventName, start: .init(), end: endDate, gradientIndex: gradientIndex)

self.events.append(event)

withAnimation {
self.showModal = false
}
}) {
RoundedRectangle(cornerRadius: 13)
.frame(maxWidth: .infinity)
.frame(height: 42)
.overlay(
Text("Add Event")
.foregroundColor(.white)
.bold()
)
}
.disabled(self.eventName.isEmpty)
}
}

}
如果有人知道如何更新 LazyVGrid 以在添加新事件时显示新事件卡,那就太棒了,谢谢!
这是问题所在:
enter image description here

最佳答案

不清楚您是如何设置 model ,但应注意更新 View ,如(scratchy)

struct ContentView: View {
@State var progress: Double = 0.0
@State var editMode: Bool = false
@State var angle: Double = 0
@State var showModal: Bool = false

@ObservedObject var model: Model // << here !!

...
或使用 @EnvironmentObject通过 .environmentObject 注入(inject)的模式
或用于 Swift 2.0
@StateObject var model: Model = Model.shared

关于ios - 关闭自定义模式/覆盖不更新 SwiftUI 中的 View 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62731781/

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