I ran into a problem while writing the program. I have a variable in the first file and buttons to change it in this file and in the second one. When you press the button in the first file, the variable changes and is displayed on the screen, and when you press the button in the second file, the variable changes but the value does not change on the screen. And both files work as a function in the third one.
Here is the code for the first file
import SwiftUI
struct S2: View {
@State var bb = 0
var body: some View {
VStack{
HStack{
Button {
aa += 1
if aa > bb {
bb += 1
}
} label: {
Text("+")
.frame(width: 140, height: 50)
.background {
RoundedRectangle(cornerRadius: 15, style: .continuous)
.fill(Color(red: 0.888, green: 0.536, blue: 0.785))
.frame(width: 140, height: 50)
}
.foregroundColor(Color(red: 1.002, green: 0.967, blue: 0.69))
.multilineTextAlignment(.center)
.bold()
.dynamicTypeSize(.accessibility1)
}
Button {
aa -= 1
if aa < bb {
bb -= 1
}
} label: {
Text("-")
.frame(width: 140, height: 50)
.background {
RoundedRectangle(cornerRadius: 15, style: .continuous)
.fill(Color(red: 0.888, green: 0.536, blue: 0.785))
.frame(width: 140, height: 50)
}
.foregroundColor(Color(red: 1.002, green: 0.967, blue: 0.69))
.multilineTextAlignment(.center)
.bold()
.dynamicTypeSize(.accessibility1)
}
}
Text(String(bb))
.frame(width: 140, height: 50)
.background {
RoundedRectangle(cornerRadius: 15, style: .continuous)
.fill(Color(red: 0.888, green: 0.536, blue: 0.785))
.frame(width: 140, height: 50)
}
.foregroundColor(Color(red: 1.002, green: 0.967, blue: 0.69))
.multilineTextAlignment(.center)
.bold()
.dynamicTypeSize(.accessibility3)
}
}
}
struct S2_Previews: PreviewProvider {
static var previews: some View {
S2()
}
}
Here is the code for the second file
import SwiftUI
struct S3: View {
var body: some View {
HStack{
Button {
aa += 1
} label: {
Text("+")
.frame(width: 140, height: 50)
.background {
RoundedRectangle(cornerRadius: 15, style: .continuous)
.fill(Color(red: 0.888, green: 0.536, blue: 0.785))
.frame(width: 140, height: 50)
}
.foregroundColor(Color(red: 1.002, green: 0.967, blue: 0.69))
.multilineTextAlignment(.center)
.bold()
.dynamicTypeSize(.accessibility1)
}
Button {
aa -= 1
} label: {
Text("-")
.frame(width: 140, height: 50)
.background {
RoundedRectangle(cornerRadius: 15, style: .continuous)
.fill(Color(red: 0.888, green: 0.536, blue: 0.785))
.frame(width: 140, height: 50)
}
.foregroundColor(Color(red: 1.002, green: 0.967, blue: 0.69))
.multilineTextAlignment(.center)
.bold()
.dynamicTypeSize(.accessibility1)
}
}
}
}
struct S3_Previews: PreviewProvider {
static var previews: some View {
S3()
}
}
Here is the code for the third file
import SwiftUI
var aa = 0
struct S1: View {
var body: some View {
TabView{
S2()
.tabItem {
Image(systemName: "person.fill")
Text("1")
}
.toolbarBackground(.visible, for: .tabBar)
.toolbarBackground(.ultraThickMaterial, for: .tabBar)
S3()
.tabItem {
Image(systemName: "person.fill")
Text("2")
}
.toolbarBackground(.visible, for: .tabBar)
.toolbarBackground(.ultraThickMaterial, for: .tabBar)
}
}
}
struct S1_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I really hope someone can help me. I haven't been able to find a way to do this for a week now.