Home > Software engineering >  How to call a function when a segment is tapped in a Segmented Picker view in SwiftUI
How to call a function when a segment is tapped in a Segmented Picker view in SwiftUI

Time:01-25

In the following code, I'm trying to save the color theme in UserDefaults using the SwiftUI @AppStorage wrapper. What I would like to do is be able to call a method to save the rawValue from the ColorTheme enum when a segment is tapped, for instance, if the System segment is tapped I would like to save nil as a String in UserDefaults, basically assign the rawValue to the appTheme wrapper inside the function, something like...

func assignTheme(segment){
    appTheme = segment.rawValue
}

How can I call a function when a segment is tapped and be able to recognize what segment was tapped to be able to assign the appropriate value to the appTheme wrapper?

import SwiftUI

enum ColorTheme: String {
    case system = "nil"
    case light = "light"
    case dark = "dark"
}

struct SegmentedView: View {
    
    @AppStorage("appTheme") private var appTheme = ColorTheme.system.rawValue
    
    var body: some View {
        VStack{
            List{
                Section(header: Text("Theme")){
                    Picker("Mode", selection: $appTheme){
                        Text("System").tag(ColorTheme.system.rawValue)
                        Text("Light").tag(ColorTheme.light.rawValue)
                        Text("Dark").tag(ColorTheme.dark.rawValue)
                    }.pickerStyle(SegmentedPickerStyle())
                        .padding()
                }
            }
        }
    }
}

struct SegmentedView_Previews: PreviewProvider {
    static var previews: some View {
        SegmentedView()
    }
}

CodePudding user response:

You can try this:

    var body: some View {
        VStack{
            List{
                Section(header: Text("Theme")){
                    Picker("Mode", selection: $appTheme.onChange(doSomething)){
                        Text("System").tag(ColorTheme.system.rawValue)
                        Text("Light").tag(ColorTheme.light.rawValue)
                        Text("Dark").tag(ColorTheme.dark.rawValue)
                    }
                    .pickerStyle(SegmentedPickerStyle())
                    .padding()

                }

            }
        }
    }

    func doSomething(_ some: String) {
        print(appTheme)
    }

onChange is here: SwiftUI Picker onChange or equivalent?

CodePudding user response:

Here is what I ended up doing.

Picker("Mode", selection: $appTheme){
    Text("System").tag(ColorTheme.system.rawValue)
    Text("Light").tag(ColorTheme.light.rawValue)
    Text("Dark").tag(ColorTheme.dark.rawValue)
}
.pickerStyle(SegmentedPickerStyle())
.padding()
.onChange(of: appTheme) {tag in
    appTheme = tag
    // call someFunction()
}
  •  Tags:  
  • Related