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)
}
}
}
}
当$selection
状态更改时,我该如何运行代码(例如print("Selection changed to \(selection)")
)? 我查看了文档,但没有找到相关内容。
print
的func
,甚至设置了断点,但没有成功。然而,当我放置SwiftUI代码(例如创建一个Text(“got here”)
)时,它可以工作。考虑到这一切都是beta1,我认为这将在以后的beta版本中得到纠正(或至少更好地记录)。 - user7014451@Published
修饰符并订阅它。 - Daniele Bernardini