0
public struct FriendlyTextField: View, BeFriend {
    public let eternalId: String
    let title: String
    @Binding var text: String

    @State var focused: Bool = false

    @StateObject var speechManager = SpeechManager.shared
    @StateObject var positionManager = PositionManager.shared

    public init(_ id: String, _ title: String, text: Binding<String>) {
        self.eternalId = id
        self.title = title
        self._text = text
    }

    public var body: some View {
        FriendlyWrappedView(eternalId) {
            TextField(title, text: $text)
                .onAppear {
                    focused = true
                    print(focused)
                }
        }
        .onRight {
            focused = true

            if !focused {
                speechManager.startRecord()
                speechManager.onRecord = eternalId
            } else {
                speechManager.stopRecord()
                speechManager.onRecord = ""
            }
        }
        .onChange(of: self.focused) {
            if !$0 {
                speechManager.stopRecord()
                speechManager.onRecord = ""
            }
        }
        .onChange(of: speechManager.text) { _ in
            if speechManager.onRecord == eternalId {
                text = speechManager.lastText + speechManager.text
            }
        }
        .onChange(of: speechManager.lastText) { _ in
            if speechManager.onRecord == eternalId {
                text = speechManager.lastText + speechManager.text
            }
        }
        .onChange(of: positionManager.focus) { newValue in
            if newValue != eternalId {
                focused = false
            }
        }
    }
}

When this view appears, it prints false, but I set focused to true. FriendlyWrappedView is just adding some modifiers to the view. Also StateObject doesn't work. The published value was changed, but onChange isn't called.

And when I change text's value, it works well.

If I change focused to a binding value, it works again

My Xcode version: 13.3 My iPad version: 15.4

Other views work well.

  • Maybe the `positionManager.focus` its triggering once the view is loaded so try to comment out your last `.onChange` block and try it. – Onix Apr 11 '22 at 13:58
  • Without a [mre], it's going to be hard to get a concrete answer to this. Especially when you say things like "StateObject" doesn't work but you don't include the code to any of the state objects. – jnpdx Apr 11 '22 at 14:20
  • What is onRight modifier ? Text is a binding so it’s changes are reported. The only time that could modify focused is onRight. – Ptit Xav Apr 11 '22 at 14:44
  • Even I delete all the not related things, it still not works – Underthestars-zhy Apr 12 '22 at 10:26
  • @jnpdx I cannot make a minimal example, because I cannot recurrent it. It works well in other situations. – Underthestars-zhy Apr 12 '22 at 10:27
  • If you can't recreate it, then it's extremely unlikely that some else will be able to recreate it, especially with missing code. – jnpdx Apr 12 '22 at 21:01

0 Answers0