关于ios:在Swift中按下特定的键盘按钮

Specific keyboard button pressed in Swift

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

如:

1
2
3
if (textField.spaceIsClicked){
  checkWords();
}


您可以通过以下方式使用UITextFieldDelegate的委托函数shouldChangeCharactersInRange

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
    }
}