【问题标题】:SwiftUI: Is there a way to create a list with single selection (like in iOS Settings)?SwiftUI:有没有办法创建一个单选列表(比如在 iOS 设置中)?
【发布时间】:2021-01-09 17:54:59
【问题描述】:

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

提前感谢您的帮助!

【问题讨论】:

标签: ios swift swiftui


【解决方案1】:

同时借助 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")

        }
    }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多