0

I'm really struggling to get a simple SwiftUI text view to update when using @State. Code below. Any help appreciated (I'm sure it's something daft but I'm new to all this - can't even copy/paste the code snippet correctly). Thank you.

struct ContentView : View {

    @State var tappedToggle: Bool = false

    var body: some View {
        Text("First: \(printRandomFactoid())").multilineTextAlignment(.center)
        .onTapGesture {
            self.tappedToggle.toggle()
            Text("Latest... \r \(printRandomFactoid())").multilineTextAlignment(.center)
        }
    }
}

1 Answer 1

2

View should never be inside action closure... The intention probably was

struct ContentView : View {

    @State var tappedToggle: Bool = false

    var body: some View {
        Text(tappedToggle ? "First: \(printRandomFactoid())" : "Latest... \r \(printRandomFactoid())")
           .multilineTextAlignment(.center)
           .onTapGesture {
              self.tappedToggle.toggle()
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.