I have a custom view that is passed a Binding to a String. The custom view renders a TextField. I can't find the right syntax to modify the binding after focus leaves the TextField. The code is something like this:
struct MyURL: View {
private let title: String
@Binding var url: String
@State private var tempUrl: String
init(_ title: String, url: Binding<String>) {
self.title = title
_url = url
_tempUrl = State(initialValue: url.wrappedValue)
}
var body: some View {
TextField(title, text: $tempUrl, onEditingChanged: editingChanged)
.autocapitalization(.none)
.disableAutocorrection(true)
}
}
private func editingChanged(hasFocus: Bool) {
guard !hasFocus else { return }
if !tempUrl.starts(with: "https://"),
!tempUrl.starts(with: "http://") {
tempUrl = "https://" + tempUrl
}
url.wrappedValue = tempUrl // THIS LINE IS WRONG!
}
}