Sheet 内容不会根据参数更新
Sheet contents won't update depending on parameters
我在按下按钮时调用 sheet。该按钮是通过 ForEach
循环生成的,我想将当前对象传递给新的 sheet。根据这个对象,我希望 sheet 显示不同的变量。
ForEach(processor.matchEvents){event in
Button(action: {showSheet.toggle()}) {
ZStack{
RoundedRectangle(cornerRadius: 15)
.fill(event.team.teamColor)
.frame(minWidth: 150, maxHeight: .infinity)
.padding([.vertical], 10)
.padding([.horizontal], 5)
VStack(alignment: .center, spacing: 5){
Group{
Text(event.team.teamName)
Text(event.time)
Text(event.event)
}
}
}
}
.sheet(isPresented: $showSheet){
EditEventView(team: event.team, event: event)
}
}
我去掉了大部分样式以帮助缩短代码。下面是我在 sheet 中尝试 运行 的代码,但没有得到新的更新事件
struct EditEventView: View {
@ObservedObject var team: Team
var event: MatchEvent
@Environment(\.presentationMode) var presentationMode
@State private var playerSelect = 0
@ObservedObject var processor = EventsProcessor.shared
var body: some View{
NavigationView{
Form{
Section(header: Text("Event")){
Text(team.teamName)
Text(event.event)
Text(event.player)
Text(event.time)
}
}
}
}
}
每个事件都存储在另一段代码中,链接如下。这样它们就可以在整个程序中访问。
struct MatchEvent: Identifiable{
var id = UUID()
var team: Team
var time: String
var event: String
var player: String
}
class EventsProcessor: ObservableObject {
static let shared = EventsProcessor()
@Published var matchEvents = [MatchEvent]()
func addEvent(time: String, event: String, player: String, team: Team){
matchEvents.append(MatchEvent(team: team, time: time, event: event, player: player))
}
}
我要显示 sheets 并且那个位正在工作,但是它会不断显示相同的数据并且不会在我参加新活动时更新。任何帮助将不胜感激。
你已经用 @ObservedObject var team: Team
以正确的方式做到了
Simply change var event: MatchEvent
to @ObservedObject var event: MatchEvent
.
@ObservedObject 属性 Wrapper 来自 Combine
并且正在“查找更新并在找到更新后更新视图”。
我在按下按钮时调用 sheet。该按钮是通过 ForEach
循环生成的,我想将当前对象传递给新的 sheet。根据这个对象,我希望 sheet 显示不同的变量。
ForEach(processor.matchEvents){event in
Button(action: {showSheet.toggle()}) {
ZStack{
RoundedRectangle(cornerRadius: 15)
.fill(event.team.teamColor)
.frame(minWidth: 150, maxHeight: .infinity)
.padding([.vertical], 10)
.padding([.horizontal], 5)
VStack(alignment: .center, spacing: 5){
Group{
Text(event.team.teamName)
Text(event.time)
Text(event.event)
}
}
}
}
.sheet(isPresented: $showSheet){
EditEventView(team: event.team, event: event)
}
}
我去掉了大部分样式以帮助缩短代码。下面是我在 sheet 中尝试 运行 的代码,但没有得到新的更新事件
struct EditEventView: View {
@ObservedObject var team: Team
var event: MatchEvent
@Environment(\.presentationMode) var presentationMode
@State private var playerSelect = 0
@ObservedObject var processor = EventsProcessor.shared
var body: some View{
NavigationView{
Form{
Section(header: Text("Event")){
Text(team.teamName)
Text(event.event)
Text(event.player)
Text(event.time)
}
}
}
}
}
每个事件都存储在另一段代码中,链接如下。这样它们就可以在整个程序中访问。
struct MatchEvent: Identifiable{
var id = UUID()
var team: Team
var time: String
var event: String
var player: String
}
class EventsProcessor: ObservableObject {
static let shared = EventsProcessor()
@Published var matchEvents = [MatchEvent]()
func addEvent(time: String, event: String, player: String, team: Team){
matchEvents.append(MatchEvent(team: team, time: time, event: event, player: player))
}
}
我要显示 sheets 并且那个位正在工作,但是它会不断显示相同的数据并且不会在我参加新活动时更新。任何帮助将不胜感激。
你已经用 @ObservedObject var team: Team
Simply change
var event: MatchEvent
to@ObservedObject var event: MatchEvent
.
@ObservedObject 属性 Wrapper 来自 Combine
并且正在“查找更新并在找到更新后更新视图”。