在文本字段中键入时不会禁用 UIKeyCommand (Swift)

UIKeyCommand is not disabled when typing into a text field (Swift)

我制作了一个计算器应用程序 (swift),我设置了一些 UIKeyCommand,这样如果用户有蓝牙键盘,他们就可以在计算器中输入 numbers/symbols。

他们是这样工作的:

UIKeyCommand(input: "4", modifierFlags: [], action: "TypeFourInCalculator:")
func TypeFourInCalculator(sender: UIKeyCommand) {
    btn4Press(UIButton)
}

一切正常,当用户按下四键时将四添加到计算器中(即使计算器本身没有文本字段)。但是:我还有几个标准文本字段,我希望 UIKeyCommands 在用户进入其中一个文本字段时停止(这样他们就可以再次使用 BT 键盘定期输入)。在不禁用 UIKeyCommands 的情况下,在计算器功能中键入结果并且在文本字段中没有输入。

所以我尝试了这个,试图在文本字段成为第一响应者时禁用 UIKeyCommands:

let globalKeyCommands = [UIKeyCommand(input: "4", modifierFlags: [], action: "TypeFourInCalculator:"), UIKeyCommand(input: "5", modifierFlags: [], action: "TypeFiveInCalculator:")]

override var keyCommands: [UIKeyCommand]? {
    if powertextfield.isFirstResponder() == false { // if the user isn't typing into that text field
        return globalKeyCommands // use key commands
    } else { // if the user is typing
        return nil // disable those UIKeyCommands
    }

这偶尔有效,但通常无效。如果用户还没有使用 BT 键盘输入任何内容(即我猜还没有激活键盘命令),那么他们可以像往常一样使用 BT 键盘在文本字段中输入。但是,如果他们已经通过 UIKeyCommand 在计算器中输入了数字,则他们无法在文本字段中输入数字(好吧,有时它可以正常工作,有时它会像我添加预防性代码之前那样失败)。键入的文本不会出现在该文本字段中,相反,它只会调用计算器命令。

那么当用户开始输入时,我该怎么做才能禁用这些 UIKeyCommand 在普通文本字段中?

您可以对 UIViewController 使用 addKeyCommand(_:)removeKeyCommand(_:) 方法,而不是将 keyCommands 计算为 属性。像这样子类化您的文本字段:

class PowerTextField: UITextField {
    var enableKeyCommands: (Bool->())?

    override func becomeFirstResponder() -> Bool {
        super.becomeFirstResponder()
        enableKeyCommands?(false)
        return true
    }

    override func resignFirstResponder() -> Bool {
        super.resignFirstResponder()
        enableKeyCommands?(true)
        return true
    }
}

然后在你的 UIViewController:

override func viewDidLoad() {
    super.viewDidLoad()

    // globalKeyCommands is a view controller property

    // Add key commands to be on by default
    for command in globalKeyCommands {
        self.addKeyCommand(command)
    }

    // Configure text field to callback when 
    // it should enable key commands
    powerTextField.enableKeyCommands = { [weak self] enabled in
        guard let commands = self?.globalKeyCommands else {
            return
        }
        if enabled {
            for command in globalKeyCommands {
                self?.addKeyCommand(command)
            }
        } else {
            for command in globalKeyCommands {
                self?.removeKeyCommand(command)
            }
        }
    }
}

您可以为 UIViewController 将采用的 UITextField 设置委托协议,而不是 UIViewController 配置的可选存储过程。

此外,如果您需要在 UIAlertController 弹出时停止这些键命令,您可以创建一个 UIAlertController 的子类来实现 viewWillAppear(_:)viewWillDisappear(_:)事件方法类似于您实现 becomeFirstResponder()resignFirstResponder() 的方式,通过调用主视图控制器在其 viewDidLoad().

期间配置的 enableKeyCommands(_:) 可选存储过程

至于为什么会发生这种情况的解释,也许最可能的解释是这是一个错误。我不确定为什么这在您的测试中是不规则的。我认为您可以尝试隔离它在什么条件下有效或无效。为什么这只会发生在蓝牙键盘上并不明显,但是当您开始引入无线技术时,可能会出现很多边缘情况。

所以我遇到了同样的问题,并找到了一个非常好的和简单的解决方案。

如果当前的 firstResponder 是文本字段或类似字段,我想出了一个办法。

extension UIResponder {
    private weak static var _currentFirstResponder: UIResponder? = nil

    public static var isFirstResponderTextField: Bool {
        var isTextField = false
        if let firstResponder = UIResponder.currentFirstResponder {
            isTextField = firstResponder.isKind(of: UITextField.self) || firstResponder.isKind(of: UITextView.self) || firstResponder.isKind(of: UISearchBar.self)
        }

        return isTextField
    }

    public static var currentFirstResponder: UIResponder? {
        UIResponder._currentFirstResponder = nil
        UIApplication.shared.sendAction(#selector(findFirstResponder(sender:)), to: nil, from: nil, for: nil)
        return UIResponder._currentFirstResponder
    }

    @objc internal func findFirstResponder(sender: AnyObject) {
        UIResponder._currentFirstResponder = self
    }
}

然后简单地使用这个布尔值来确定在覆盖 keyCommands var:

时 return 哪个 UIKeyCommands
    override var keyCommands: [UIKeyCommand]? {
        var commands = [UIKeyCommand]()

        if !UIResponder.isFirstResponderTextField {
            commands.append(UIKeyCommand(title: "Some title", image: nil, action: #selector(someAction), input: "1", modifierFlags: [], propertyList: nil, alternates: [], discoverabilityTitle: "Some title", attributes: [], state: .on))

        }

        commands.append(UIKeyCommand(title: "Some other title", image: nil, action: #selector(someOtherAction), input: "2", modifierFlags: [.command], propertyList: nil, alternates: [], discoverabilityTitle: "Some title", attributes: [], state: .on))

        return commands
    }