簡體   English   中英

Swift Eureka Forms-如何為字段選項分配值?

[英]Swift Eureka Forms - How to assign values to field options?

如果我這樣創建分段的行字段:

<<< SegmentedRow<String>(){ row in
    row.title = "Sex"
    row.tag = "sex"
    row.selectorTitle = "Select your sex"
    row.options = ["Male","Female"]
}

如何將選項與特定值匹配? 例如,如果用戶選擇“男性”,那么有沒有辦法在代碼中獲得M而不是“ Male ,但是仍然向用戶顯示“ Male ”?

或者,例如,如果我有一個國家/地區列表:

<<< PushRow<String>(){ row in
  row.title = "Passport Issuing Country"
  row.tag = "passportIssuingCountry"
  row.selectorTitle = "Passport Issuing Country"
  row.options = ["AUSTRALIA","AUSTRIA","BELGIUM"]
}

我可以將每個國家/地區名稱分配給國家/地區代碼,例如AUSTRALIA返回AUAUSTRIA返回AT ,等等。

目前,無法保留期權的內部價值-但這是您可以做的:

1.使用所有可用選項創建一個枚舉

enum Sex: Int {
    case NotKnown       = 0
    case Male           = 1
    case Female         = 2
    case NotApplicable  = 9

    static let all      = [NotKnown, Male, Female, NotApplicable]
    static let strings  = ["Unknown", "Male", "Female", "Not Applicable"]

    func string() -> String {
        let index = Sex.all.index(of: self) ?? 0
        return Sex.strings[index]
    }

    static func fromString(string: String) -> Sex {
        if let index = Sex.strings.index(of: string) {
            return Sex.all[index]
        }
        return Sex.NotKnown
    }
}

2.使用要顯示的所有選項創建行

<<< SegmentedRow<String>(){ row in
    row.title = "Sex"
    row.tag = "sex"
    row.selectorTitle = "Select your sex"
    row.options = [Sex.Female.string(), Sex.Male.string()]
}

3.讀取值

let row: SegmentedRow<String>? = self.form.rowBy(tag: "sex")
if let value = row?.value {
    // Returns "Female"
    print(value)

    // Returns 2
    print(Sex.fromString(string: value).rawValue)
}

用戶可以看到並選擇Strings ,但是會得到一個enum值,例如可以將它作為整數保存在數據庫中(請參閱:ISO / IEC 5218)。

https://zh.wikipedia.org/wiki/ISO/IEC_5218

暫無
暫無

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

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