簡體   English   中英

SwiftUI - Picker 有兩組 init - 第二組是做什么用的? 我如何獲得選定的項目?

[英]SwiftUI - Picker has two sets of init - what are the second set for? and how do i get selected item?

我正在詳細了解 SwiftUI 控件並為每個 init 編寫示例代碼。

Picker 有兩組 init。

一個你從數組等中填充你的列表,你選擇的東西進入選擇:綁定。

但是第二組 init 是做什么用的呢? 它們在“為集合創建選擇器”部分下,我可以使用這兩個初始化來填充集合中的數組

但是你如何使用第二組 inits 獲得選定的項目? selection: param 不再是對 ivar 的綁定,而是填充列表的 Keypath。 我的問題是如何使用第二組 inits 獲取所選項目。

在這里查看初始化:

https://developer.apple.com/documentation/swiftui/picker

對於 Picker 有 6 個初始化。

  • 3 在“創建選擇器”下

    • 這些都可以。 例如,我從一個數組中填充列表,並將所選項目存儲在由選擇指定的單個結果中:param。 它將結果綁定到一個 ivar。
  • 'Creating a picker for a collection'下還有3個初始化

我得到這個來顯示集合中的項目,例如我修改了蘋果文檔中的示例代碼。 文檔中的代碼無法編譯,因此 apple 可能丟失了一些東西。

    import SwiftUI

    enum Thickness: String, CaseIterable, Identifiable {
       case thin
       case regular
       case thick
    
        var id: String { rawValue }

    }

    //to use in ist must be Hashable
    struct Border: Identifiable  {
        var color: Color
        var thickness: Thickness
    
        //Identifiable > Hashable > id > String
        //var id: String { return "\(color.hashValue)" }
    
        let id = UUID()
    }
    extension Color{
        func colorName() -> String{
            if self == Color.black{
                 return "black"
            }
            else if self == Color.red{
                return "red"
            }
            else{
               return "UNHANDLED"
            }
        }
    }



    struct CLCPickers_selection_FromCollection_View: View {
    
    @State private var selectedObjectBorders = [
        Border(color: .black, thickness: .thin),
        Border(color: .red, thickness: .thick)
    ]
    

    var body: some View {
        VStack{
            //------------------------------------------------------------------
            Picker(
                "Border Thickness",
                sources: $selectedObjectBorders,
                selection: \.thickness
            ) {
                //------------------------------------------------------------------
                //I added
                //id: \.self
                //Picker: the selection "thin" is invalid and does not have an associated tag, this will give undefined results.
                //------------------------------------------------------------------
                ForEach(Thickness.allCases,
                        id: \.self)
                { thickness in
                    Text(thickness.rawValue)
                }
            }

            //------------------------------------------------------------------
            Divider()
            //------------------------------------------------------------------
            //This just lists the colors in the arrays of Border
            
            //QUESTION - how do I find out the currenly selected one?
            //normaly selection: in the picker would be bound to the picked item
            //but for this init selection: is a keypath
            //selection: \.thickness
            //so I can fill the Picker list using the keypath into the Border array.
            //BUT HOW DO I FIND OUT THE CURRENTLY SELECTED ITEM?
            //theres no binding? 
            //is there a .selectedItem property some where?
            
            List(selectedObjectBorders) {
                Text("\($0.color.colorName())")
                
            }
        }
    }
}

問題已得到解答,但發帖者出於某種原因將其刪除。

答案:這個選擇器初始化設置了集合中每個邊界 object 的 thinkness ivar。

要查看更改,我應該顯示結果以顯示 thickness.rawvalue 以查看每個邊框的更改 object

    List(selectedObjectBorders) { border in
        HStack{
            Text("\(border.color.colorName())")
            Text("\(border.thickness.rawValue)") //<<- will change when you select an item. All will match.
    }
}

暫無
暫無

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

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