0

I have the following kind of code in a SwiftUI app.

And I want to perform a certain action (which is going to update my UI) when the state variable answerFlag has been touched (in the CustomButton view).

I would like to get some hints on the way to go for that.

My SwiftUI knowledge is still too limited or I do not yet master the use of what I know. The couple of things I tried did not work.

struct ContentView: View {
    @State var answerFlag:Bool = false
    ......

    var body: some View {
        VStack {
            Spacer()
            CustomButton(text: answerText,
                         validAnswer: correctAnswer,
                         replyFlag: $answerFlag)
            Spacer()
        }.background(theBGColor)
            .onTapGesture {
                ... do some useful thing ...
            }
        // I need to perform some action when answerFlag has changed !!??
    }
}


struct CustomButton: View {
    var text,validAnswer: String
    @Binding var replyFlag:Bool

    var body: some View {
        Text(text)
        .font(.largeTitle)
        .fontWeight(.bold)
        .foregroundColor(.primary)
        .padding(.horizontal, 6.0)
        .background(Color.brown)
        .cornerRadius(5.0)
        .overlay(RoundedRectangle(cornerRadius: 5.0)
        .stroke(lineWidth: 2.0)
        .foregroundColor(Color.gray))
        .onTapGesture {
            print("BUTTON-HIT:\(self.text)::\(self.validAnswer)")
            if self.text == self.validAnswer {print("OK")}
            else {print("NG")}
            self.replyFlag = true
        }
    }
}
2
  • Your question is a little too vague. What kind of action did you have in mind? The following have different answers: 1) I want to update my model with stats on right/wrong counts, 2) I want to display an alert saying “correct!”, 3) I want to display a Text saying “That is correct!” Commented Aug 29, 2020 at 11:59
  • I would also look into didSet. Look at this link and ctrl + f for "didSet" docs.swift.org/swift-book/LanguageGuide/Properties.html Commented Aug 29, 2020 at 17:41

1 Answer 1

1

Here is possible way...

SwiftUI 2.0

var body: some View {
    VStack {
        Spacer()
        CustomButton(text: answerText,
                     validAnswer: correctAnswer,
                     replyFlag: $answerFlag)
        Spacer()
    }.background(theBGColor)
        .onTapGesture {
            ... do some useful thing ...
        }
    // I need to perform some action when answerFlag has changed !!??
        .onChange(of: answerFlag) { newValue in
            // ... do anything heeded here
        }
}

SwiftUI 1.0+

Similarly to above add to some view the following

import Combine

    ...
    .onReceive(Just(answerFlag)) { newValue in
        // ... do anything heeded here
    }

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.