首页 > 解决方案 > SwiftUI:有没有办法创建一个单选列表(比如在 iOS 设置中)?

问题描述

我目前正在使用 Swift UI 构建我的第一个 iOS 应用程序,我想知道 Swift UI 中是否有任何标准方式允许在列表中进行单选。就像 iOS 设置中的情况一样(见下面的截图)。

1

提前感谢您的帮助!

标签: iosswiftswiftui

解决方案


同时通过 hackingwithswift.com 上的示例代码找到了答案

struct ContentView: View {
var strengths = ["Mild", "Medium", "Mature"]

@State private var selectedStrength = 0

var body: some View {
    NavigationView {
        Form {
            Section {
                Picker(selection: $selectedStrength, label: Text("Strength")) {
                    ForEach(0 ..< strengths.count) {
                        Text(self.strengths[$0])

                        }
                    }
                }
            }.navigationBarTitle("Select your cheese")

        }
    }
}

推荐阅读