Posts

Post not yet marked as solved
5 Replies
2.1k Views
Hi, So in iOS14, I've been using a code pattern to listen for changes in TextField's text bound value and to reject/drop unwanted changes and values. In iOS15, this pattern no longer seems to work , but I've not found others complaining about it, so I'm now wondering is iOS15's behaviour a bug, or just the expected behavior? A simplified example of this type of code pattern is shown below which when run on: iOS14 effectively disables the use of the "3" button. iOS15 Xcode Beta 2 does not, instead even though source of truth "textFieldText" reflects the expected value, the TextField displays what is typed regardless. I've put FB9290496 , thoughts etc welcome. Thanks import SwiftUI class No3ViewModel: ObservableObject { // Xcode 13 beta 2 - iOS14 prevents the entry and display of the digit "3" in the TextField // that uses it. While in iOS15 it does not (even when the backing store has the correct value). private var textFieldTextBacking: String = "" var textFieldText: String { get { textFieldTextBacking } set { /// Prevent infinite loops guard newValue != textFieldTextBacking else { return } /// Prevent setting input that contrains a "3" and force a rething of anythig that has speculatively done that, guard !newValue.contains("3") else { objectWillChange.send() return } textFieldTextBacking = newValue objectWillChange.send() } } } struct ContentView: View { @StateObject var vm = No3ViewModel() var label: String { vm.textFieldText == "" ? "Enter anything except 3" : "not set"} var body: some View { return VStack { Text("I reject the number 3 - my backing value is = \"\(vm.textFieldText)\"") if #available(iOS 15.0, *) { TextField(label, text: $vm.textFieldText, prompt: Text("Bum")) .keyboardType(.decimalPad) .textFieldStyle(RoundedBorderTextFieldStyle()) } else { // Fallback on earlier versions TextField(label, text: $vm.textFieldText) .keyboardType(.decimalPad) .textFieldStyle(RoundedBorderTextFieldStyle()) } } .padding([.leading, .trailing], 16) } }
Posted Last updated
.