Reputation: 57
I am trying to make a second view on an app which is like a leaderboard. When the game has completed 10 rounds on ContentView, I want the calculated score to be stored and printed on the ScoreboardView (accessed by a button). I used @Binding to connect the score variable from one view to the other, but keep getting the "out of scope" error. Does anyone know why this is? Here is my code for the ScoreboardView:
'''
import SwiftUI
struct scoreboardView: View {
@Binding var score: Int
var body: some View {
List {
ForEach(1..<9) {
Text("Game \($0): \(score) ")
}
}
}
}
struct scoreboardView_Previews: PreviewProvider {
static var previews: some View {
scoreboardView(score: $scoreTracker)
}
}
'''
This is not my final code, therefore ignore the middle. However, I get the error in the last line of the initializing of the preview.
Upvotes: 0
Views: 4785
Reputation: 52475
You don't have anything defined called scoreTracker
in your code, but you're trying to use it in your preview. Instead, you can pass a constant in place of your binding (just for preview purposes):
struct scoreboardView: View {
@Binding var score: Int
var body: some View {
List {
ForEach(1..<9) {
Text("Game \($0): \(score) ")
}
}
}
}
struct scoreboardView_Previews: PreviewProvider {
static var previews: some View {
scoreboardView(score: .constant(50))
}
}
Upvotes: 2