Post

Replies

Boosts

Views

Activity

Reply to SwiftUI view update problem
In fact, same occurs without Binding in MyView2. That's because when a State var changes, SwiftUI reevaluates everything that depends on this State var (and does not reevaluate the full body per se). See details here: https://stackoverflow.com/questions/66635304/how-swiftui-detects-there-are-state-variable-values-passed-to-subviews I posted an example based on your code. May be we'll get even deeper insight into SwiftUI.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’24
Reply to SwiftUI view update problem
That's how SwiftUI works. With the Binding on v1 in MyView1 and MyView2, each time you change the State variable v1 (through Bing in MyView1), that propagates to MyView2 through its own Binding. Note: no need to use wrappedValue. This works: Button(action: { v1 = 10 }, label: { Text("OK") }) as well as if v1 == 0 { Text("v1 = \(v1) v2 = \(v2) ") } else { Text("???") }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’24
Reply to It doesn't work with the code to bind the buttons to the following Views.
Problem was mainly how ForEach and NavigationLink where intertwined : https://stackoverflow.com/questions/66017531/swiftui-navigationlink-bug-swiftui-encountered-an-issue-when-pushing-anavigatio/67626758#67626758 Here is a code that works (even though some error messages I had no time to clear): struct Item: Identifiable, Hashable { var id = UUID() var name: String var color: Color var image: String } struct ContentView: View { @State var navigatedItem: String? @State var isActivated = false @ViewBuilder private func selectedView(name: String) -> some View { switch name { case "Medical": MedicalView() case "Illness": IllnessView() case "Vaccune": VaccuneView() case "Dewor": DeworView() case "Allergie": AllergieView() default: EmptyView() } } var body: some View { NavigationView { ZStack(){ Color(.systemGray6).ignoresSafeArea() VStack() { ScrollView(.horizontal, showsIndicators: false) { HStack(spacing: 5) { ForEach(MockData.items, id:\.self) { item in ZStack { Button(action:{ navigatedItem = item.name isActivated = true print(item.name) }) { RoundedRectangle(cornerRadius: 10) .foregroundStyle(item.color.self) .frame(width: 70, height: 70) } Image(systemName: item.image) .foregroundColor(.white) .font(.system(size: 30)) .padding(20) } } .background(){ NavigationLink(destination: selectedView(name: navigatedItem ?? "Medical"), isActive: $isActivated) { EmptyView() } } } //HStack //opciones } // cierre scrollview .padding(.top, 20) .padding(.leading) Spacer() } //cierre Vstack } //cierre de Zstack .navigationTitle("Caracteristicas") .toolbar{ ToolbarItem(placement: .navigationBarLeading) { Button(action:{}, label: { Image(systemName: "switch.2")}) } ToolbarItem(placement: .navigationBarTrailing) { Button(action:{}, label: {Image(systemName: "person.circle")}) } }//toolBar .accentColor(.red) } } } struct MockData { static var items = [ Item3(name: "Medical" ,color: .red, image:"heart"), Item3(name: "Illness" ,color: .blue, image:"pill"), // Illness Item3( name:"Vaccune" ,color: .orange, image: "syringe"), Item3(name: "Dewor" ,color: .green, image: "microbe"), Item3(name: "Allergie" ,color:.purple, image: "allergens")] } struct MedicalView: View { var body: some View { Text("Medical") } } struct IllnessView: View { var body: some View { Text("Illness") } } struct VaccuneView: View { var body: some View { Text("Vaccune") } } struct DeworView: View { var body: some View { Text("Dewor") } } struct AllergieView: View { var body: some View { Text("Allergie") } }
Topic: App & System Services SubTopic: Core OS Tags:
Apr ’24
Reply to Arranging Controller, Scenes & Views Structure
At first I thought, it would be a good idea to arrange multiple scenes, all controlled by the GameViewController, due to not having to duplicate recurring methods or so. But as I thought of, I saw a GameViewController file bigger and bigger and I had the fear to more and more loosing the focus the more scenes I added. It depends how similar are the functions in your scenes. But you can declare the function at the top level (outside any class) and call them from Views. Doing so, GameController will just segue to the appropriate View. Or you could put the fund in a 'Utils' class (maybe as static function) and call Utils.theFunc (if static) or create a Utils instance (utils: Utils) in each Scene (which will allow to adapt some properties) and call utils.myFunc. Hard to say more without knowing more on your code.
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’24
Reply to app store review rejected
Is it a question to other developers ? If so, what is the question ? If it is for Apple, the forum is not the right channel, you'd better either: add information in reply to reviewer in Appstore post a bug report contact support. In both cases, you should provide much more detailed information (which standard are you speaking of ? Which png image ?)
Apr ’24
Reply to how to calculate this logarithmic operation
Welcome to the forum. Some simple math here. Your expression is: 10 log (2 * 10**(x/10)) That is 10 log(2) + 10 log (10**(x/10) = 10 log(2) + 10 * x / 10 * log(10) So result is simply: 10 log(2) + x log(10) = 6.931472 + 2.3025851 x Works for log or log10 as you want. That's much simpler (and gives a slightly more accurate result because there are less rounding effects) than the brute force computation): var x : Float = 15 // Set the value of x func compute(_ x: Float) -> Float { let x1 = x / 10 let lnx = x1 * log(10) let p = exp(lnx) // 10 ** (x/10) // log is Neper log, not log10 let r = 10 * log(p + p) return r } print(compute(x)) let r = 10*log(2) + x*log(10) print(r) And check you get exactly the same result… If that answers your question, don't forget to close the thread by marking the correct answer. Otherwise explain what you don't understand.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’24
Reply to OrderedDictionary encode issue
I don't understand your post. You say: does anyone know why the output of OrderedDictionary is not like this: {"bbb": 12, "ccc": 13, "ddd": 14, "bbc": 15} But just before you wrote: The output JSON of OrderDictionary is like this: [ "bbb", 12, "ccc", 13, "ddd", 14, "bbc", 15 ] What is the point ? To have an array and not a dictionary ? See discussion here: https://forums.swift.org/t/ordereddictionary-decoding/66829
Topic: UI Frameworks SubTopic: UIKit Tags:
Apr ’24
Reply to Wrong information found in SwiftUI documentation!
Welcome to the forum. There are many other similar examples of code samples in documentation that are not uptodate. Don't loose too much time on this. You should file a bug report against documentation. And anyway it is detected by compiler with suggestion on how to correct or with auto completion advice.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’24
Reply to App design and set up
I hope I understand exactly what you want to achieve. You don't show all code, but you should have a first View (often named ContentView). It looks like the following: struct ContentView: View { // var declarations, Sate var… @State var showPicker = true // at start only @State var selectedStagione = "" // You could also set the initial value 2023/2024 here without need for onAppear var body: some View { VStack(alignment: .leading) { // the view content, with the picker for idStagione selection if showPicker { // display the Picker Picker("Please select", selection: $selectedStagione) { // build Picker items } .onChange(of: selectedStagione) { _, _ in // The new format for onChange showPicker = false print("Selection done") } } } .onAppear { // set idStagione initial value here selectedStagione = "2023/2024" // adapt as needed } } } Please tell if you need more explanation ; if so, show your ContentView. If OK, don't forget to close the thread by marking the correct answer.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’24