2 Star 23 Fork 10

小弟调调/swiftui-example

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
README.md 1.59 KB
一键复制 编辑 原始数据 按行查看 历史

如何创建分段控件并从中读取值?

SwiftUI 的 Picker 也可以用来创建与 UIKit 中的 UISegmentedControl 等效的分段控件,尽管它需要绑定到某种状态,并且您必须确保为每个分段提供一个标签,以便于对其进行识别。 段可以是文本或图片; 其他任何事情都会悄无声息地失败。

例如,这将创建一个与 favouriteColorColor 状态属性一起使用的分段控件,并在下面添加一个文本视图,该文本视图显示所选的任何值:

struct ContentView: View {
    @State private var favoriteColor = 0

    var body: some View {
        VStack {
            Picker(selection: $favoriteColor, label: Text("What is your favorite color?")) {
                Text("Red").tag(0)
                Text("Green").tag(1)
                Text("Blue").tag(2)
            }
            .pickerStyle(SegmentedPickerStyle())

            Text("Value: \(favoriteColor)")
        }
    }
}

不过,在这种情况下,最好创建一个数组来存储各种颜色,然后使用 ForEach 通过循环在内部创建文本视图:

struct ContentView: View {
    @State private var favoriteColor = "Red"
    var colors = ["Red", "Green", "Blue"]

    var body: some View {
        VStack {
            Picker(selection: $favoriteColor, label: Text("What is your favorite color?")) {
                ForEach(colors, id: \.self) {
                    Text($0)
                }
            }
            .pickerStyle(SegmentedPickerStyle())

            Text("Value: \(favoriteColor)")
        }
    }
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Swift
1
https://gitee.com/jaywcjlove/swiftui-example.git
git@gitee.com:jaywcjlove/swiftui-example.git
jaywcjlove
swiftui-example
swiftui-example
main

搜索帮助