关闭自定义模式/覆盖不更新 SwiftUI 中视图的内容
Dismissing custom modal / overlay not updating contents of View In SwiftUI
更新:我稍微重构一下以使用@ObservableObject
,但问题仍然存在
在我的 SwiftUI 应用程序中,我有一个 LazyVGrid
填充了 n 卡片和一个 New Event 按钮。当我按下 New Event 按钮时,我有一个自定义模式覆盖视图以让用户添加新事件。但是,在关闭视图后(通过点击 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)
}
}
}
}
这是 添加事件 按钮的视图:
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 以在添加新事件时显示新事件卡,那就太好了,谢谢!
这是问题所在:
不清楚你是如何设置的model
,但应该观察更新视图,因为(刮擦)
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
进行注入
或 Swift 2.0 使用
@StateObject var model: Model = Model.shared
更新:我稍微重构一下以使用@ObservableObject
,但问题仍然存在
在我的 SwiftUI 应用程序中,我有一个 LazyVGrid
填充了 n 卡片和一个 New Event 按钮。当我按下 New Event 按钮时,我有一个自定义模式覆盖视图以让用户添加新事件。但是,在关闭视图后(通过点击 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)
}
}
}
}
这是 添加事件 按钮的视图:
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 以在添加新事件时显示新事件卡,那就太好了,谢谢!
这是问题所在:
不清楚你是如何设置的model
,但应该观察更新视图,因为(刮擦)
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
或 Swift 2.0 使用
@StateObject var model: Model = Model.shared