从模型中获取数字。

huangapple go评论68阅读模式
英文:

Swift Getting Numbers From A Model

问题

当我按下按钮时,我希望文本上移1、2、3、4、5,一切都很顺利,直到我尝试创建一个模型,现在当我按下按钮时,视图上什么都不会发生。它只保持在0,不会上升。如何将文本更改为上升1、2、3、4、5?

import SwiftUI

struct ContentView: View {
    
    @State private var statistic = Statistic()
    
    var body: some View {
        ZStack(alignment: .center){
            Color("BColor")
                .ignoresSafeArea()
            VStack(alignment: .center, spacing: 100){
                Spacer()
                ImagesView()
                TextView(textAzul: String(statistic.marcadorAzul), textRojo: String(statistic.marcadorRojo))
                ScoreButtons(statistic: $statistic) // Pass the statistic binding to ScoreButtons
                if statistic.marcadorRojo > statistic.marcadorAzul{
                    Text("El juego todavía sigue y el equipo Rojo está ganando.")
                        .fixedSize(horizontal: false, vertical: true)
                        .font(.headline)
                        .fontWeight(.regular)
                        .multilineTextAlignment(.center)
                }
                else if statistic.marcadorRojo < statistic.marcadorAzul{
                    Text("El juego todavía sigue y el equipo Azul está ganando.")
                        .fixedSize(horizontal: false, vertical: true)
                        .multilineTextAlignment(.center)
                        .font(.headline)
                        .fontWeight(.regular)
                }
                else if statistic.marcadorRojo == 0 || statistic.marcadorAzul == 0 {
                    Text("El juego todavía no ha empezado, te avisaremos.")
                        .multilineTextAlignment(.center)
                        .font(.headline)
                        .fontWeight(.regular)
                }
                else if statistic.marcadorRojo == statistic.marcadorAzul{
                    Text("El juego todavía sigue y está empatado.")
                        .multilineTextAlignment(.center)
                        .font(.headline)
                        .fontWeight(.regular)
                }
                Spacer()
            }
        }
    }
}

struct ScoreButtons: View{
    
    @Binding var statistic: Statistic // Use a binding to update the statistic
    
    var body: some View{
        HStack(spacing: 135){
            VStack (alignment: .center, spacing: 10){
                Button(action: {
                    statistic.marcadorAzul += 1
                },
                       label: {
                    Image(systemName: "plus.circle")
                })
                .font(.largeTitle)
                .fontWeight(.regular)
                .foregroundColor(Color("ButtonColor"))
                Button(action: {
                    statistic.marcadorAzul -= 1
                    if statistic.marcadorAzul < 0 {
                        statistic.marcadorAzul = 0
                    }
                },
                       label: {
                    Image(systemName: "minus.circle")
                })
                .font(.largeTitle)
                .fontWeight(.regular)
                .foregroundColor(Color("ButtonColor"))
            }
            VStack(alignment: .center, spacing: 10) {
                Button(action: {
                    statistic.marcadorRojo += 1
                },
                       label: {
                    Image(systemName: "plus.circle")
                })
                .font(.largeTitle)
                .fontWeight(.regular)
                .foregroundColor(Color("ButtonColor"))
                Button(action: {
                    statistic.marcadorRojo -= 1
                    if statistic.marcadorRojo < 0 {
                        statistic.marcadorRojo = 0
                    }
                },
                       label: {
                    Image(systemName: "minus.circle")
                })
                .font(.largeTitle)
                .fontWeight(.regular)
                .foregroundColor(Color("ButtonColor"))
            }
        }
    }
}

// Rest of the code remains the same...

这段代码修改了 ScoreButtons 结构体以使用 @Binding 来更新 statistic,这样可以使得按下加号或减号按钮时更新数据。其他部分保持不变。

英文:

When I press the buttons I want the text to go up 1,2,3,4,5 and Everything was fine until I tried to make a model and get the numbers from a model. Now when I press the buttons, nothing happens on the view side. It just stays in 0 and don't go up

Context View Code

//
//  ContentView.swift
//  ExatlonApp
//
//  Created by Alperen Sarışan on 26.05.2023.
//
import SwiftUI
struct ContentView: View {
@State private var statistic = Statistic()
var body: some View {
ZStack(alignment: .center){
Color(&quot;BColor&quot;)
.ignoresSafeArea()//Background rengi
VStack(alignment: .center, spacing: 100){
Spacer()
ImagesView()
TextView(textAzul: String(statistic.marcadorAzul), textRojo: String(statistic.marcadorRojo))
ScoreButtons()
if statistic.marcadorRojo &gt; statistic.marcadorAzul{
Text(&quot;El juego todavia sigue y el equipo Rojo esta ganando.&quot;)
.fixedSize(horizontal: false, vertical: true)
.font(.headline)
.fontWeight(.regular)
.multilineTextAlignment(.center)
}
else if statistic.marcadorRojo &lt; statistic.marcadorAzul{
Text(&quot;El juego todavia sigue y el equipo Azul esta ganando.&quot;)
.fixedSize(horizontal: false, vertical: true)
.multilineTextAlignment(.center)
.font(.headline)
.fontWeight(.regular)
}
else if statistic.marcadorRojo == 0 || statistic.marcadorAzul == 0 {
Text(&quot;El juego todavia no empezo, te avisaremos.&quot;)
.multilineTextAlignment(.center)
.font(.headline)
.fontWeight(.regular)
}
else if statistic.marcadorRojo == statistic.marcadorAzul{
Text(&quot;El juego todavia sigue y esta empatada.&quot;)
.multilineTextAlignment(.center)
.font(.headline)
.fontWeight(.regular)
}
Spacer()
}
}
}
}
struct ScoreButtons: View{
@State private var statistic = Statistic()
var body: some View{
HStack(spacing: 135){
VStack (alignment: .center, spacing: 10){
Button(action: {
statistic.marcadorAzul += 1
if statistic.marcadorAzul &lt;= 11{
statistic.marcadorAzul = 0
}
},
label: {
Image(systemName: &quot;plus.circle&quot;)
})
.font(.largeTitle)
.fontWeight(.regular)
.foregroundColor(Color(&quot;ButtonColor&quot;))
Button(action: {
statistic.marcadorAzul -= 1
if statistic.marcadorAzul &lt;= -1{
statistic.marcadorAzul = 0
}
},
label: {
Image(systemName: &quot;minus.circle&quot;)
})
.font(.largeTitle)
.fontWeight(.regular)
.foregroundColor(Color(&quot;ButtonColor&quot;))
}
VStack(alignment: .center, spacing: 10) {
Button(action: {
statistic.marcadorRojo += 1
if statistic.marcadorRojo &gt;= 11{
statistic.marcadorRojo = 0
}
},
label: {
Image(systemName: &quot;plus.circle&quot;)
})
.font(.largeTitle)
.fontWeight(.regular)
.foregroundColor(Color(&quot;ButtonColor&quot;))
Button(action: {
statistic.marcadorRojo -= 1
if statistic.marcadorRojo &lt;= -1{
statistic.marcadorRojo = 0
}
},
label: {
Image(systemName: &quot;minus.circle&quot;)
})
.font(.largeTitle)
.fontWeight(.regular)
.foregroundColor(Color(&quot;ButtonColor&quot;))
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

TextView

//
//  TextView.swift
//  ExatlonApp
//
//  Created by Alperen Sarışan on 26.05.2023.
//
import SwiftUI
struct TextView: View {
var textAzul: String
var textRojo: String
var body: some View {
HStack(spacing: 100){
Text(textAzul)
.frame(width: 35, height: 35, alignment: .center)
.font(.title)
.fontWeight(.regular)
.padding()
.overlay(
Circle()
.stroke(Color.orange, lineWidth: 3)
.padding(1)
)
//Azul
Text(textRojo)
.frame(width: 35, height: 35, alignment: .center)
.font(.title)
.fontWeight(.regular)
.padding()
.overlay(
Circle()
.stroke(Color.orange, lineWidth: 3)
.padding(1)
)
//Rojo
}
}
}
struct TextView_Previews: PreviewProvider {
static var previews: some View {
TextView(textAzul: &quot;0&quot;, textRojo: &quot;0&quot;)
}
}

Model

//
//  Statistic.swift
//  ExatlonApp
//
//  Created by Alperen Sarışan on 26.05.2023.
//
import Foundation
import SwiftUI
struct Statistic{
var marcadorRojo = 0
var marcadorAzul = 0
}

Everything was working fine until I tried to make a model, Every time I press the plus button I want the text go up 1,2,3,4 but It's not working. How can I change the text to up to 1,2,3,4,5

答案1

得分: 0

问题在于ContentView中的@State private var statistic = Statistic()ScoreButtons中的@State private var statistic = Statistic()不共享相同的数据源,因此更新其中一个不会更新或反映在另一个上。

一种解决方案是通过binding的概念创建它们共享的一个数据源。

您可以在这里了解更多信息:什么是@Binding属性包装器?

因此,将ScoreButtons视图更改为接受一个绑定:

@State private var statistic = Statistic()更改为@Binding var statistic: Statistic

并在调用ScoreButtons视图的地方将ScoreButtons()更改为ScoreButtons(statistic: $statistic)

通过使用绑定,对ScoreButtons视图内部的statistic值所做的更改也会反映在视图外部,反之亦然。它建立了视图与值之间的双向连接,允许视图同时读取和修改该值。

英文:

The issue here is that the @State private var statistic = Statistic() in your ContentView and the one in your ScoreButtons do not share the same data source, so updating one does not update or reflect on the other one.
One solution is to create one data source they share through the concept of binding.

You can learn more here: What is the @Binding property wrapper?

So, Change ScoreButtons view to accept a binding:

Convert @State private var statistic = Statistic()

to @Binding var statistic: Statistic

And where you call the ScoreButtons view, change ScoreButtons() to ScoreButtons(statistic: $statistic)

By using a binding, changes made to the statistic value inside the ScoreButtons view will also reflect outside of the view, and vice versa. It establishes a two-way connection between the view and the value, allowing the view to both read and modify the value.

huangapple
  • 本文由 发表于 2023年5月28日 04:39:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定