2016-12-17 60 views
0

我删除@UIApplicationMain中的AppDelegate.swift并写下如下的main.swift,但程序仍然无法启动keyPressed函数。但是每个keydown都会执行print("send event2")如何拦截swift iOS应用程序的关键事件?

如何启动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 
    } 
} 

回答

0

我修改 '选择' 是#selector(SWIFT 3),终于解决了这个问题,也许有一个bug

+0

“行动:#selector(的keyPressed(发件人:))” 而不是“操作:选择器((“keyPressed:”))“ – baileymiller2017

相关问题