【问题标题】:SwiftUI and enum in picker选择器中的 SwiftUI 和枚举
【发布时间】:2021-02-14 20:19:55
【问题描述】:

我在游乐场工作,试图学习 SwiftUI。我正在创建一个视图,其中包含一个标签、一个文本字段、用于显示选取器和选取器值的文本。

我目前拥有的是这个

import UIKit
import PlaygroundSupport
import SwiftUI

enum Units: String, CaseIterable, Identifiable
{
    case Inches = "in"
    case Millimeters = "mm"
    case Centimeters = "cm"
    
    var id: String { self.rawValue }
}

struct VariableView: View
{
    let name: String
    let labelValue: String
    @State private var inputValue = ""
    @State private var inputUnits = Units.Inches
    
    init(name: String, label: String)
    {
        self.name = name
        labelValue = label
    }
    
    var body: some View
    {
        VStack(alignment: .leading, spacing: 0)
        {
            Text(self.labelValue)
                .font(.subheadline)
                .frame(width: .infinity, alignment: .leading)
                .padding(.horizontal, 4)
            HStack
            {
                TextField("Value", text: $inputValue)
                    .padding(.horizontal, 4)
                    .keyboardType(.numbersAndPunctuation)
                Text(inputUnits.id)
                Picker("...", selection: $inputUnits)
                {
                    /*
                    Text("Inches").tag(Units.Inches)
                    Text("Millimeters").tag(Units.Millimeters)
                    Text("Centimeters").tag(Units.Centimeters)
                     */
                    ForEach(Units.allCases)
                    {
                        unit in Text(unit.rawValue.capitalized)
                    }
                }
                .padding(10)
                .pickerStyle(MenuPickerStyle())
            }
            Divider()
             .frame(height: 1)
             .padding(.horizontal, 30)
             .background(Color.black)
        }
    }
}

// Make a SwiftUI view
struct ContentView: View
{
    var body: some View
    {
        VariableView(name: "value1", label: "Value 1")
        VariableView(name: "value1", label: "Value 2")
        VariableView(name: "value1", label: "Value 3")
        VariableView(name: "value1", label: "Value 4")
        VariableView(name: "value1", label: "Value 5")
    }
}

// Make a UIHostingController
let viewController = UIHostingController(rootView: ContentView())

// Assign it to the playground's liveView
PlaygroundPage.current.liveView = viewController

当我单击“...”时,它会崩溃。变为黑色并停止运行。没有错误,只是停止。

如果我使用三个文本行(当前已注释掉)而不是 ForEach,它可以工作 - 但最终我需要能够为每个输入动态设置允许的单位,所以我需要 ForEach 工作。

【问题讨论】:

  • 这看起来像是 Playground 限制(可能是选择器菜单样式)。此代码在模拟器中运行良好。
  • 尝试模拟器或真实设备 - 在这里可以正常工作。我认为这是 Playground 的错误。
  • 谢谢大家。我以为我快疯了。可以在模拟器中运行游乐场吗?
  • @jbmillard 的答案有用吗?

标签: swift xcode swiftui


【解决方案1】:

inputTypes 的类型是 Units,硬编码 Texts 上的标签与该类型匹配。在 ForEach 中,标签是 Units.rawValue,类型是 String。看看这是否适用于 Playground:

struct PickFromEnumView: View {
    @State private var inputUnits = Units.none
    
    var body: some View {
        VStack {
            Picker(inputUnits.rawValue, selection: $inputUnits) {
                ForEach(Units.allCases) { unit in
                    unit.label.tag(unit)
                }
            }
            .padding(10)
            .pickerStyle(MenuPickerStyle())
            Spacer()
        }
    }
    
    enum Units: String, CaseIterable, Identifiable
    {
        case none = "…"
        case inches = "in"
        case millimeters = "mm"
        case centimeters = "cm"
        
        var id: String { self.rawValue }
        
        var label : some View {
            switch(self) {
            case .none: return Label("…", systemImage: "circle")
            case .inches: return Label("inch", systemImage: "1.circle")
            case .millimeters: return Label("millimeter", systemImage: "2.circle")
            case .centimeters: return Label("centimeter", systemImage: "3.circle")
            }
        }
    }
}

【讨论】:

  • 谢谢你,但它也崩溃了。我把这个归结为游乐场的虫子。我让它在模拟器中工作。
猜你喜欢
  • 1970-01-01
  • 2020-01-10
  • 1970-01-01
  • 1970-01-01
  • 2021-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-03
相关资源
最近更新 更多