相关疑难解决方法(0)

How can I run an action when a state changes?

enum SectionType: String, CaseIterable {
    case top = "Top"
    case best = "Best"
}

struct ContentView : View {
    @State private var selection: Int = 0

    var body: some View {
        SegmentedControl(selection: $selection) {
            ForEach(SectionType.allCases.identified(by: \.self)) { type in
                Text(type.rawValue).tag(type)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

How do I run code (e.g print("Selection changed to \(selection)") when the $selection state changes? I looked through the docs and I couldn't find anything.

state ios swift swiftui

8
推荐指数
5
解决办法
2167
查看次数

标签 统计

ios ×1

state ×1

swift ×1

swiftui ×1