如何拦截swift iOS应用程序的关键事件?

时间:2016-12-17 09:28:04

标签: swift events key uikeycommand

我删除了@UIApplicationMain中的AppDelegate.swift并编写了main.swift,如下所示,但该程序仍然无法触发keyPressed功能。但是每个keydown执行print("send event2")

如何解雇keyPressed功能?这个程序有问题吗?

import UIKit
import Foundation

class TApplication: UIApplication {
    override func sendEvent(_ event: UIEvent!) {
       super.sendEvent(event)
    }

    override var keyCommands: [UIKeyCommand]? {
      print("send event2") // this is an example
        return [
            UIKeyCommand(input: "1", modifierFlags: [], action: Selector(("keyPressed:")), discoverabilityTitle: “1”),
            UIKeyCommand(input: "2", modifierFlags: .shift, action: Selector("keyPressed:"), discoverabilityTitle: “2”)]
        ]
    }

    func keyPressed(sender: UIKeyCommand) {
        print("keypressed event !!!!!!!!") // this is an example
    }
}

1 个答案:

答案 0 :(得分:0)

我将'selector'修改为#selector(swift 3)并最终解决了问题,也许有一个bug