【问题标题】:Swift keyboard selection from @IBInspectable来自 @IBInspectable 的 Swift 键盘选择
【发布时间】:2020-03-14 20:29:29
【问题描述】:

我在 Swift 中为文本字段使用键盘选择,我需要在 @IBInspectable 中显示枚举名称 (emailAdress...) 而不是其 Int 编号 (0-6)。我该如何解决这个问题?

  public class TextFieldView: UIView{

     enum KeyboardType: Int {
            case normal
            case asciiCapable
            case numberPad
            case phonePad
            case emailAddress
            case namePhonePad
        }

      @IBInspectable public var keyBoard: Int = 0 {
            didSet {
                let keyBoardType = KeyboardType(rawValue: keyBoard)
                self.textField.keyboardType = UIKeyboardType.init(rawValue: keyBoardType!.rawValue)!

            }
    }

【问题讨论】:

标签: swift textfield


【解决方案1】:

使用 CustomStringConvertible

enum KeyboardType: Int, CustomStringConvertible {
     case normal = 0
     case asciiCapable = 1
     case numberPad = 2
          ...

     var description: String {
          switch self {
       case .normal:
          return "normal"
       case .asciiCapable:
          return "asciiCapable"
           ...
          }
    }

【讨论】:

    猜你喜欢
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    相关资源
    最近更新 更多