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
}
}
「行動:#selector(的keyPressed(發件人:))」 而不是「操作:選擇器((「keyPressed:」))「 – baileymiller2017