UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Update state variable in multiple views

Forums > SwiftUI

@Jonne  

Hi,

I'm working on the 100 days of Swiftui and because I'm having trouble grasping some of the content I've learned I started doodling with a simple app to test what I've learned. But I'm having trouble with the concept of updating a state variable in multiple views. To demonstrate my problem I made a simple example.

InsertView.swift

import SwiftUI

struct InsertView: View {

    @State private var number: Int = 0

    var body: some View {
        Button("Add to number") {
            number += 1
        }

        Text("Initial: \(number)")
    }
}

#Preview {
    InsertView()
}

ContentView.swift

import SwiftUI

struct ContentView: View {

    @State var number: Int

    var body: some View {
        InsertView()
        Text("Controle: \(number)")
    }
}

#Preview {
    ContentView(number: 0)
}

In this example both numbers should be the same when pressing the button and incrementing the number. I hope someone can help me understand this and point me in the right direction.

   

The ContentView owns your variable number. Therefore, you have to use a Binding in your InserView. So you don't use @State in your InsertView but @Binding. Currently, your InsertView and ContentView don't have a link for these two different variables. But you don't want two different variables. That's why you use Binding. When you use @Binding you don't give the variable an initial value but you when you call InsertView you do it InsertView(number: $number). The $ represents that you use a Binding.

2      

@Jonne  

Thank you @Hatsushira, it works in my testcase.

InsertView.swift

import SwiftUI

struct InsertView: View {

    @Binding var number: Int

    var body: some View {
        Button("Add to number") {
            number += 1
        }

        Text("Initial: \(number)")
    }
}

#Preview {
    @State var number = 0
    return InsertView(number: $number)
}

ContentView.swift

import SwiftUI

struct ContentView: View {

    @State var number: Int

    var body: some View {
        InsertView(number: $number)
        Text("Controle: \(number)")
    }
}

#Preview {
    @State var number = 0
    return ContentView(number: number)
}

Hopefully I can implement this in the project.

   

You're welcome.

In ContentView you can give an initial value to number if you like.

1      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.