美文网首页
SwiftUI 替换UISegmentController和Se

SwiftUI 替换UISegmentController和Se

作者: 大_瓶_子 | 来源:发表于2020-09-03 10:03 被阅读0次

效果图:


截屏2020-09-03 10.08.13.png
import SwiftUI

struct ContentView: View {
    let personArr: Array<String> = ["小学", "初中", "高中", "大学", "社会人"]
    @State var selectedIndex = 0
    var body: some View {
        VStack{
            Text(personArr[selectedIndex])
            Picker(selection: $selectedIndex, label: Text("")) {
                ForEach(0 ..< personArr.count) { index in
                    Text(self.personArr[index])
                }
            }
            .pickerStyle(SegmentedPickerStyle())
            .padding()
            .onReceive([self.selectedIndex].publisher.first()) { (value) in
                //获取到当前点击的下标 do something
                print(value)
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

相关文章

网友评论

      本文标题:SwiftUI 替换UISegmentController和Se

      本文链接:https://www.haomeiwen.com/subject/utdnsktx.html