没有修饰符标志的键命令-Swift 2 [英] Key Commands with no modifier flags—Swift 2
问题描述
我在swift中构建了一个计算器,我希望拥有蓝牙键盘的iPad用户可以在没有任何屏幕键盘干扰的情况下进行打字。我认为最好的方法是使用 UIKeyCommand
s,但我遇到了一个问题:使用objective-c你可以做类似这样的事情 [UIKeyCommand keyCommandWithInput:@fmodifierFlags:0 action:@selector(keyPressF)]
,在swift中不能有'0'修饰符标志。
I built a calculator in swift, and I would like an iPad user who has a bluetooth keyboard be able to type without any onscreen keyboard distractions. I think the best way to do this is with UIKeyCommand
s, but I am running into a problem: whereas with objective-c you can do something like this [UIKeyCommand keyCommandWithInput:@"f" modifierFlags:0 action:@selector(keyPressF)]
, in swift there can not be '0' modifier flags.
请回答其中一个!
当用户如何在swift中实现目标在没有文本字段作为第一响应者的情况下在iPad蓝牙键盘上键入某个字符,而不按控制键或选项键?
某事当我按下command-j时会发生。当我按下我的iPad BT键盘上的4键时,我能做些什么吗?
推荐答案
我可以帮忙你有这两个。
I can help you with both of these.
-
在Swift 2.0及以上版本中你必须传递一个空数组而不是0。初始化程序工作看起来像:
UIKeyCommand(输入:f,modifierFlags:[],action:keyPressF)
。原因是因为0未定义在UIKeyModifierFlags结构中的任何地方。这在UIKit中很常见,所以习惯将这个模式用于空选项是个好主意。
In Swift 2.0 and above you have to pass an empty array instead of 0. To make your initializer work it would look like:
UIKeyCommand(input: "f", modifierFlags: [], action: "keyPressF")
. The reason why is because 0 isn't defined anywhere in the UIKeyModifierFlags struct. This is pretty common practice in UIKit so it's a good idea to get used to using this pattern for empty options.
是的!在初始化程序中输入输入:4
而不是输入:j
。
Yes! Just pass input: "4"
in your initializer instead of input: "j"
.
这篇关于没有修饰符标志的键命令-Swift 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!