在 Swift 中按下了特定的键盘按钮

Specific keyboard button pressed in Swift

是否可以在按下键盘上的特定按钮时调用函数?

如:

if (textField.spaceIsClicked){
  checkWords();
}

您可以这样使用 UITextFieldDelegate 的委托函数 shouldChangeCharactersInRange

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var textF: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        textF.delegate = self

    }
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        if string == " " {
            //Run your function here
            println("SpaceBar is pressed")
        }
        return true
    }
}