Post

Replies

Boosts

Views

Activity

Reply to TextField("x", value: $y, format: .currency(code: "USD")) Behaves oddly
I would do it with a text format: struct ContentView: View { @State private var usdAmount = 0.0 @State private var usd = "0" var body: some View { VStack { Text("Currency Form") Form { // TextField("Dollar Amount", value: $usdAmount, format: .currency(code: "USD")) // 1 TextField("Dollar Amount", value: $usdAmount, format: .number) // 2 .keyboardType(.numberPad) TextField("Dollar Amount", text: $usd) // text format .keyboardType(.numberPad) .onTapGesture { usd = "" } .onSubmit { usdAmount = Double(usd) ?? 0.0 usd = "$ " + usd } } Text("usdAmount = \(usdAmount) \(usd)") } .padding() } }
Topic: UI Frameworks SubTopic: SwiftUI
Jun ’24
Reply to SwiftUI app runs differently on hardware platforms
I tested on MacMini, MacOS 14.5, Xcode 15.3. For what it's worth. Do you get this error in log: Picker: the selection "0" is invalid and does not have an associated tag, this will give undefined results. Replacing @State var psel:Int = 0 by @State var psel:Int = 1 clears the error I also get this error when selecting in Picker: CLIENT ERROR: TUINSRemoteViewController does not override -viewServiceDidTerminateWithError: and thus cannot react to catastrophic errors beyond logging them
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’24
Reply to Can't typing on NSTextField
So the test shows that showSuggestionMenu removes focus. I didn't say that was the solution, just a way of testing. So now, as I suggested before, try to reset focus after updateSuggestionMenu() showSuggestionMenu()
Topic: UI Frameworks SubTopic: AppKit Tags:
Jun ’24
Reply to Moving focus between textfields using enter key
Use FocusState. Here is a simple code example: struct ContentView: View { let textCase1 = 1 let textCase2 = 2 @State var text1: String = "" @State var text2: String = "" @FocusState private var focusedField: Int? var body: some View { VStack { HStack { Text(" TextField 1") TextField("", text: $text1) .border(Color.blue, width: 1) .focused($focusedField, equals: textCase1) .onSubmit { focusedField = 2 } } HStack { Text(" TextField 2") TextField("", text: $text2) .border(Color.red, width: 1) .focused($focusedField, equals: textCase2) .onSubmit { focusedField = 1 } } Button { focusedField = nil print("No more focus") } label: { Text("Do something else") } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’24
Reply to Closure | Error when referring static variable
You can refer to a static variable. As shown in the small code below: struct Preview: View { private static let NAME = "Test" var array: [String] = ["someTest", "Hello", "HelloTest"] var filtered: [String] { array.filter { $0.contains(Preview.NAME) } } var body: some View { ForEach(filtered, id: \.self) { s in Text(s) } } } So the problem is the use in the Query. May be you could try to include in a computed var ?
Topic: Programming Languages SubTopic: Swift Tags:
Jun ’24
Reply to Share settings from SwiftUI in-app settings view to other views
There are several options: use environment var, in a class that groups all the settings AppStorage may be a good option. However, if you have a lot of var, that becomes tedious. How many var have you, of which type ? An idea here is to multiplex the var. imagine you have 8 Int var, which value is in fact between 0 and 255. Then you can create a function to multiplex them: var1 + 256*var2 * 256*256*var3 etc and a demux function, using a succession of % and DIV to get each var back The best would be you show some code so that we can be more specific.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’24