The number on the screen does not change when the variable changes.

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.

Accepted Reply

You should make aa a State var aa = 0 in S1 and make it a @Binding var aa: Int in both S2 & S3.

Replies

You should make aa a State var aa = 0 in S1 and make it a @Binding var aa: Int in both S2 & S3.

import SwiftUI

struct S1: View {
    @State var aa = 0
    
    var body: some View {
        TabView{
            S2(aa: $aa)
            .tabItem {
                Image(systemName: "person.fill")
                Text("1")
            }
            .toolbarBackground(.visible, for: .tabBar)
            .toolbarBackground(.ultraThickMaterial, for: .tabBar)
            S3(aa: $aa)
                .tabItem {
                    Image(systemName: "person.fill")
                    Text("2")
                }
                .toolbarBackground(.visible, for: .tabBar)
                .toolbarBackground(.ultraThickMaterial, for: .tabBar)
        }
    }
}

struct S1_Previews: PreviewProvider {
    static var previews: some View {
        S1()
    }
}
import SwiftUI

struct S2: View {
    @State var bb = 0
    @Binding var aa: Int
    
    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)
        }
        .onChange(of: aa) { newValue in
            if aa > bb {
                bb += 1
            } else if aa < bb {
                bb -= 1
            }
        }
    }
}
struct S2_Previews: PreviewProvider {
    static var previews: some View {
        S2(aa: .constant(0))
    }
}
import SwiftUI

struct S3: View {
    @Binding var aa: Int
    
    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(aa: .constant(0))
    }
}
  • Thank you very much! This decision helped a lot, put an end to my weeks of suffering. I hope the question was not very stupid, since I am writing my first application and there is very little information on the Internet on Xcode 14.

Add a Comment