0

I have a Text view that acts as a button. When the user taps it it has to disable a TextField.

I set the boolean variable that enables and disables the TextField, inside an .onTapGesture() modifier. I did that so the state of the variable wasn't being modified directly inside the body of a view, which is what would cause the error.

However, I'm still getting the error when I tap on the Text view.

Commenting out "answerDisabled = true", stops the error but obviously doesn't disable the TextField.

Text("Show next time")
    .font(.system(size: buttonFontSize, weight: .heavy, design: .rounded))
    .padding()
    .background(RoundedRectangle(cornerRadius: 10, style: 
    .continuous).fill(showNextTimeDisabled ? .gray : Color.systemBlue))
    .foregroundColor(.white)
    .opacity(showNextTimeDisabled ? 0.8 : 1.0)
    .disabled(showNextTimeDisabled)
    .frame(width: geometry.size.width * buttonWidthScale, height: geometry.size.height * buttonHeightScale)
    .position(x: geometry.frame(in: .local).midX + geometry.size.width / 3.9, y: geometry.frame(in: .local).midY + geometry.size.height / 26)
    .onTapGesture {
          spacedRepetition.decreaseTestsUntilPresented(currentQuestion: testSectionThings?[questionCounter] ?? SectionThing())
          showNextTimeDisabled = true
          iKnowThisDisabled = true
          answerDisabled = true
          skipDisabled = false
                    
          if answered {
             testSectionThings?[questionCounter].correctAnswers = 0
             testSectionThings?[questionCounter].attempts += 1
             PersistenceController.shared.saveDB()
          }
                    
          if wrongAnswer.isEmpty {
             let separator = NSMutableAttributedString(string: " ", attributes: noTestSectionThingsAttributes)
             let tapAttributedString = NSMutableAttributedString(string: "Tap ", attributes: noTestSectionThingsAttributes)
             let skipImageAttachment = NSTextAttachment()
             skipImageAttachment.image = UIImage(named: "skip.png")?.withTintColor(.systemBlue)
             let skipImageAttributedString = NSMutableAttributedString(attachment: skipImageAttachment)
             let nextQuestionAttributedString = NSMutableAttributedString(string: NSLocalizedString(" for next question.", comment: "Answer field tap skip 2"), attributes: noTestSectionThingsAttributes)
             let NSMutableAttributedStringArray = [tapAttributedString, skipImageAttributedString, nextQuestionAttributedString]
             nativeForeignPlaceholder = NSMutableAttributedStringArray.join(withSeparator: separator)
          }
     }
Tirna
  • 383
  • 1
  • 12
  • Welcome to SO - Please take the [tour](https://stackoverflow.com/tour) and read [How to Ask](https://stackoverflow.com/help/how-to-ask) to improve, edit and format your questions. Without a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) it is impossible to help you troubleshoot. – lorem ipsum Oct 14 '22 at 13:31
  • Does this answer your question? [textFieldDidChangeSelection: Modifying state during view update, this will cause undefined behavior](https://stackoverflow.com/questions/58878980/textfielddidchangeselection-modifying-state-during-view-update-this-will-cause) – workingdog support Ukraine Oct 15 '22 at 03:43

0 Answers0