繁体   English   中英

在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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM