Swift从模型中获取数字

guz6ccqo  于 2023-06-04  发布在  Swift
关注(0)|答案(1)|浏览(125)

当我按下按钮时,我希望文本上升1,2,3,4,5,一切都很好,直到我试图制作一个模型,并从一个模型中获得数字。现在当我按下按钮时,视图端什么也没有发生。它只是停留在0,不上升
上下文视图代码

//
//  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("BColor")
                .ignoresSafeArea()//Background rengi
            VStack(alignment: .center, spacing: 100){
                Spacer()
                ImagesView()
                TextView(textAzul: String(statistic.marcadorAzul), textRojo: String(statistic.marcadorRojo))
                ScoreButtons()
                if statistic.marcadorRojo > statistic.marcadorAzul{
                    Text("El juego todavia sigue y el equipo Rojo esta ganando.")
                        .fixedSize(horizontal: false, vertical: true)
                        .font(.headline)
                        .fontWeight(.regular)
                        .multilineTextAlignment(.center)
                }
                else if statistic.marcadorRojo < statistic.marcadorAzul{
                    Text("El juego todavia sigue y el equipo Azul esta ganando.")
                        .fixedSize(horizontal: false, vertical: true)
                        .multilineTextAlignment(.center)
                        .font(.headline)
                        .fontWeight(.regular)
                }
                else if statistic.marcadorRojo == 0 || statistic.marcadorAzul == 0 {
                    Text("El juego todavia no empezo, te avisaremos.")
                        .multilineTextAlignment(.center)
                        .font(.headline)
                        .fontWeight(.regular)
                }
                else if statistic.marcadorRojo == statistic.marcadorAzul{
                    Text("El juego todavia sigue y esta empatada.")
                        .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 <= 11{
                        statistic.marcadorAzul = 0
                    }
                },
                       label: {
                    Image(systemName: "plus.circle")
                })
                .font(.largeTitle)
                .fontWeight(.regular)
                .foregroundColor(Color("ButtonColor"))
                Button(action: {
                    statistic.marcadorAzul -= 1
                    
                    if statistic.marcadorAzul <= -1{
                        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
                    
                    if statistic.marcadorRojo >= 11{
                        statistic.marcadorRojo = 0
                    }
                },
                       label: {
                    Image(systemName: "plus.circle")
                })
                .font(.largeTitle)
                .fontWeight(.regular)
                .foregroundColor(Color("ButtonColor"))
                Button(action: {
                    statistic.marcadorRojo -= 1
                    
                    if statistic.marcadorRojo <= -1{
                        statistic.marcadorRojo = 0
                    }
                },
                       label: {
                    Image(systemName: "minus.circle")
                })
                .font(.largeTitle)
                .fontWeight(.regular)
                .foregroundColor(Color("ButtonColor"))
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

文本视图

//
//  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: "0", textRojo: "0")
    }
}

型号

//
//  Statistic.swift
//  ExatlonApp
//
//  Created by Alperen Sarışan on 26.05.2023.
//

import Foundation
import SwiftUI

struct Statistic{
    
    var marcadorRojo = 0
    var marcadorAzul = 0

}

一切都很好,直到我试着做一个模型,每次我按下加号按钮,我想文本上升1,2,3,4,但它不工作。如何将文本更改为最多1,2,3,4,5

kkbh8khc

kkbh8khc1#

这里的问题是ContentView中的@State private var statistic = Statistic()ScoreButtons中的@State private var statistic = Statistic()不共享同一个数据源,因此更新其中一个数据源不会更新或反映另一个数据源。一种解决方案是通过binding的概念创建一个共享数据源。
您可以在这里了解更多信息:What is the @Binding property wrapper?
因此,更改ScoreButtons视图以接受绑定:
转换@State private var statistic = Statistic()
@Binding var statistic: Statistic
在调用ScoreButtons视图的地方,将ScoreButtons()更改为ScoreButtons(statistic: $statistic)
通过使用绑定,对ScoreButtons视图内部的statistic值所做的更改也将反映到视图外部,反之亦然。它在视图和值之间建立双向连接,允许视图读取和修改值。

相关问题