2016-12-17 61 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

相關問題