英文:
Similar code. One works another not? Why?
问题
无法找到问题。请帮忙。
有2个页面。第一个完美运行,表(AnalysisGraph)也在工作。
第二个不行。数据没有更改,传递给(AnalysisGraph或ShowGraph - ShowGraph与AnalysisGraph相同,只是没有功能的)。为什么在第一个页面中,我可以在saveAnalysis中更改数据并传递给AnalysisGraph,但在第二个页面中无法更改数据?
对于长代码感到抱歉。
第一页
@State var analizaId = ""
@State var finance = 0.0
@State var value = 0.0
@State var growth = 0.0
.sheet(isPresented: $newAnalysis) {
AnalysisGraph(clearAnalysis: clear, analizaId: analizaId, finance: finance, value: value, growth: growth, name: name, notes: notes)
}
private func saveAnalysis(){
finance = model.financeCondition()
growth = model.growthCondition()
value = model.valueCondition()
analizaId = model.modelId
}
第二页
@State var analizaId = ""
@State var finance = 0.0
@State var value = 0.0
@State var growth = 0.0
.sheet(isPresented: $showAnalysisGraph) {
ShowGraph(analizaId: analizaId, finance: finance, value: value, growth: growth, name: name, notes: notes)
}
private func setDataToGraph(){
if let idAnalysis = selected{
for (_, item) in analysisList.enumerated(){
if let idItem = item.id{
if idItem == idAnalysis{
thisAnaliza = item
}
}
}
}
if let showAnaliza = thisAnaliza, let idOptionalAnaliza = selected, let idAnaliza = idOptionalAnaliza{
print("idAnaliza: \(idAnaliza)")
analizaId = idAnaliza
finance = showAnaliza.financeAnalysis
growth = showAnaliza.growthAnalysis
value = showAnaliza.valueAnalysis
name = showAnaliza.name ?? "No Name"
notes = showAnaliza.notes ?? "No notes"
print("analizaId: \(analizaId)")
print("\(finance)")
print("\(value)")
print("\(growth)")
print(name)
print(notes)
showAnalysisGraph.toggle()
}
}
英文:
I cannot find the issue. Please help.
There are 2 pages. First is working perfectly and the sheet (AnalysisGraph) is working
On second is not. Data is not changed and empty data is passed to (AnalysisGraph or ShowGraph - ShowGraph is like AnalysisGraph only without function)
Why in 1 page I can change data in saveAnalysis and passed that to AnalysisGraph but I cannot change data in 2 page?
Sorry for long code
1 page
@State var analizaId = ""
@State var finance = 0.0
@State var value = 0.0
@State var growth = 0.0
.sheet(isPresented: $newAnalysis) {
AnalysisGraph(clearAnalysis: clear, analizaId: analizaId, finance: finance, value: value, growth: growth, name: name, notes: notes)
}
}
private func saveAnalysis(){
finance = model.financeCondition()
growth = model.growthCondition()
value = model.valueCondition()
analizaId = model.modelId
}
2 page
@State var analizaId = ""
@State var finance = 0.0
@State var value = 0.0
@State var growth = 0.0
.sheet(isPresented: $showAnalysisGraph) {
ShowGraph(analizaId: analizaId, finance: finance, value: value, growth: growth, name: name, notes: notes)
}
private func setDataToGraph(){
if let idAnalysis = selected{
for (_, item) in analysisList.enumerated(){
if let idItem = item.id{
if idItem == idAnalysis{
thisAnaliza = item
}
}
}
}
if let showAnaliza = thisAnaliza, let idOptionalAnaliza = selected, let idAnaliza = idOptionalAnaliza{
print("idAnaliza: \(idAnaliza)")
analizaId = idAnaliza
finance = showAnaliza.financeAnalysis
growth = showAnaliza.growthAnalysis
value = showAnaliza.valueAnalysis
name = showAnaliza.name ?? "No Name"
notes = showAnaliza.notes ?? "No notes"
print("analizaId: \(analizaId)")
print("\(finance)")
print("\(value)")
print("\(growth)")
print(name)
print(notes)
showAnalysisGraph.toggle()
}
}
答案1
得分: 1
在第2页,仅当状态被设置时才会调用body,前提是这些先前的状态已在body内部被读取。在sheet内部不计算。
尝试用1个可选的项目结构state(封装您的4个变量)替换这4个状态,并使用sheet(item:)。
英文:
In 2 page, body is only called when states are set if those previous states have been read inside body. Inside sheet doesn't count.
Try replacing the 4 states with 1 optional item struct state (encapsulating your 4 vars) and use sheet(item:).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论