我制作了一个计算器应用程序( UIKeyCommands ),并设置了一些,这样如果用户有蓝牙键盘,他们就可以在计算器中输入数字/符号。
它们的工作方式如下:
UIKeyCommand(input: "4", modifierFlags: [], action: "TypeFourInCalculator:")
func TypeFourInCalculator(sender: UIKeyCommand) {
btn4Press(UIButton)
}所有这些都很好,当用户按下四个键时,在计算器中添加一个4(即使计算器本身没有文本字段)。但是:我也有两个标准文本字段,当用户进入其中一个文本字段时,我希望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在计算器中输入了数字,那么他们就不能在文本字段中键入数字(嗯,有时候它适用于正常行为,有时会失败,就像我添加预防代码之前那样)。类型化文本只是不出现在该文本字段中,而是调用计算器命令。
,那么当用户开始输入普通文本字段时,我能做什么来禁用这些UIKeyCommands呢?
发布于 2016-06-01 05:52:03
与使keyCommands成为计算属性不同,您可以使用addKeyCommand(_:)和removeKeyCommand(_:)方法对UIViewController的文本字段进行以下子类:
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子类,该子类实现与实现becomeFirstResponder()和resignFirstResponder()类似的viewWillAppear(_:)和viewWillDisappear(_:)事件方法,方法是调用主视图控制器在其viewDidLoad()期间配置的enableKeyCommands(_:)可选存储过程。
至于为什么会发生这种情况,最有可能的解释是它是一个bug。我不知道为什么在你的测试中这是不正常的。我认为你可以试着把它在什么条件下起作用或不起作用。不清楚为什么这只会发生在蓝牙键盘上,但当你开始引入无线技术时,会有很多边缘情况出现。
发布于 2019-10-11 13:41:06
因此,我面临着同样的问题,找到了一个非常好和简单的解决方案。
我想出了一种方法,即当前的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
}
}然后简单地使用这个布尔值来确定在重写UIKeyCommands var时返回哪个keyCommands:
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
}https://stackoverflow.com/questions/37356815
复制相似问题