簡體   English   中英

SwiftUI 和選擇器中的枚舉

[英]SwiftUI and enum in picker

我在操場上工作,試圖學習 SwiftUI。 我正在創建一個視圖,其中包括 label、一個文本字段、顯示選取器和選取器值的文本。

到目前為止我所擁有的是這個

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 工作。

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")
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM