How can I run an action when a state changes?

后端 未结 7 2221
醉话见心
醉话见心 2020-12-05 22:25
enum SectionType: String, CaseIterable {
    case top = \"Top\"
    case best = \"Best\"
}

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

           


        
7条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 23:09

    In iOS 14 there is now a onChange modifier you can use like so:

    SegmentedControl(selection: $selection) {
        ForEach(SectionType.allCases.identified(by: \.self)) { type in
            Text(type.rawValue).tag(type)
        }
    }
    .onChange(of: selection) { value in
        print("Selection changed to \(selection)")
    }
    

提交回复
热议问题