由于某些原因,当我更改来自另一个视图的值时,它似乎不会更新主视图上的值.数据存储在一个可观察的对象中,数组被发布,所以我不确定是什么问题.
我希望更改这些值以匹配数据数组中的值(如下所列).
以下是数据的存储方式
import Foundation
import SwiftUI
struct PlayerData: Identifiable {
let id: Int
let name: String
var balance: Int
let iconname: String
var active: Bool
let accentColor: Color
}
class PlayerDataViewModel: ObservableObject {
@Published var PlayerDataList: [PlayerData] = [
PlayerData(id: 0, name: "Player1", balance: 1500, iconname: "car.side.fill", active: true, accentColor: .red),
PlayerData(id: 1, name: "Player2", balance: 1500, iconname: "ferry.fill", active: true, accentColor: .green),
PlayerData(id: 2, name: "Player3", balance: 1500, iconname: "airplane", active: true, accentColor: .green),
PlayerData(id: 3, name: "Player4", balance: 1500, iconname: "fan.ceiling.fill", active: true, accentColor: .white)
]
}
下面是显示这些值的主视图代码
import SwiftUI
struct MainScreenView: View {
@ObservedObject var viewModel:PlayerDataViewModel
@Binding var SelectedPlayerIndex: Int
var body: some View {
NavigationView {
VStack {
Text("Game")
.font(.largeTitle)
.fontWeight(.bold)
HStack {
NavigationLink(destination: FocusedBalanceView(SelectedPlayerIndex: 0, viewModel: PlayerDataViewModel())) {
VStack(spacing: 10) {
Text("Values")
.font(.title2)
.fontWeight(.semibold)
.foregroundColor(.white)
HStack {
VStack(spacing: 10) {
ForEach(viewModel.PlayerDataList) { item in
if(item.active == true) {
Image(systemName: item.iconname)
.foregroundColor(item.accentColor)
}
}
}
VStack(spacing: 10) {
ForEach(viewModel.PlayerDataList) { item in
if(item.active == true) {
Text("\(item.balance)")
.foregroundColor(.white)
}
}
}
}
}
.padding()
.background(.ultraThinMaterial)
.cornerRadius(8)
}
}
}
}
.navigationBarBackButtonHidden(true)
}
}
编辑:这是更改主视图中的值的代码
import SwiftUI
struct FocusedBalanceView: View {
@State var SelectedPlayerIndex: Int
@ObservedObject var viewModel: PlayerDataViewModel
var body: some View {
VStack {
Form {
Section(header: Text("Balance")) {
Picker("Balance", selection: $SelectedPlayerIndex) {
Text("").tag("")
ForEach(viewModel.PlayerDataList) { item in
if item.active == true {
HStack {
Text("\(item.name)").tag("\(item.id)")
Image(systemName: "\(item.iconname)")
}
}
}
}
}
}
.frame(height: 280)
VStack {
Text("Current Balance")
.font(.title3)
.fontWeight(.semibold)
Text("\(viewModel.PlayerDataList[SelectedPlayerIndex].balance)")
.font(.title2)
.fontWeight(.bold)
}
.padding()
.background(.ultraThinMaterial)
.cornerRadius(8)
VStack {
Text("Actions")
.font(.title2)
.fontWeight(.semibold)
HStack {
Button(action: {
viewModel.PlayerDataList[SelectedPlayerIndex].balance += 500
}) {
Image(systemName: "car")
.frame(width: 70, height: 70)
.foregroundColor(.white)
.fontWeight(.bold)
.font(.title)
.background(.green)
.clipShape(Circle())
}
Button(action: {
viewModel.PlayerDataList[SelectedPlayerIndex].balance -= 500
}) {
Image(systemName: "dollarsign")
.frame(width: 70, height: 70)
.foregroundColor(.white)
.fontWeight(.bold)
.font(.title)
.background(.red)
.clipShape(Circle())
}
Button(action: {
viewModel.PlayerDataList[SelectedPlayerIndex].balance -= 250
}) {
Image(systemName: "mappin.and.ellipse")
.frame(width: 70, height: 70)
.foregroundColor(.white)
.fontWeight(.bold)
.font(.title)
.background(.blue)
.clipShape(Circle())
}
}
}
.padding()
.background(.ultraThinMaterial)
.cornerRadius(8)
}
}
}
希望这已经足够了,如果还不够,那就告诉我.