5

我正在尝试在 Swift 3 中采用UITextInputTraits自定义子类。UIButton

我添加了文档“符号”部分中指定的所有属性:

import UIKit 

class TextInputButton: UIButton {
    var autocapitalizationType: UITextAutocapitalizationType = .none
    var autocorrectionType: UITextAutocorrectionType         = .no
    var spellCheckingType: UITextSpellCheckingType           = .no
    var enablesReturnKeyAutomatically: Bool                  = false
    var keyboardAppearance: UIKeyboardAppearance             = .default
    var keyboardType: UIKeyboardType                         = .default
    var returnKeyType: UIReturnKeyType                       = .default
    var isSecureTextEntry: Bool                              = false

    // (...rest of class implementation omitted...)

但是,当我尝试编译时,isSecureTextEntry会导致以下错误:

由 setter 为 'isSecureTextEntry' 提供的 Objective-C 方法 'setIsSecureTextEntry:' 与要求的选择器 ('setSecureTextEntry:') 不匹配

如果我使用建议的“修复它”,属性声明将变为:

var @objc(setSecureTextEntry:) isSecureTextEntry: Bool = false

...但现在我得到了三个错误:

  1. 预期模式

  2. 一行中的连续声明必须用';'分隔

var(要我在和之间插入分号@objc),

和:

  1. 预期声明

实现这一属性的正确方法是什么?

4

1 回答 1

2

也许不完全是你的情况,但 UITextInputTraits 的这种采用对我来说效果很好

(次要提示:UIKeyInput 扩展了 UITextInputTraits)

public class ShortPasswordEntry : UIControl, UIKeyInput
{
    public override init(frame: CGRect) {
        super.init(frame: frame)
    }

    extension ShortPasswordEntry
    {
        public var keyboardType: UIKeyboardType {
            get {
                return .numberPad
            }
            set {
                assertionFailure()
            }
        }
    }
}
于 2017-10-19T07:54:55.553 回答