如何在Swift中将NSButton的keyEquivalent设置为NSDownArrowFunctionKey

时间:2015-03-30 22:42:51

标签: macos cocoa swift nsbutton

如何在Swift中将向下箭头键设置NSButton的keyEquivalent(这是一种String类型)? NSDownArrowFunctionKey是一个Int。

1 个答案:

答案 0 :(得分:2)

Apple的文档中的page解决了这个问题。他们提供的示例是用Objective-C编写的,Swift等价如下:

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!
    @IBOutlet weak var button: NSButton!

    func applicationDidFinishLaunching(aNotification: NSNotification) {

        var array = [unichar(NSDownArrowFunctionKey)]
        button.keyEquivalent = NSString(characters: array, length: 1)
    }
}