ios - 在 Swift 中按下特定的键盘按钮

标签 ios xcode swift function keyboard

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

如:

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

最佳答案

您可以这样使用 UITextFieldDelegate 的委托(delegate)函数 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
    }
}

关于ios - 在 Swift 中按下特定的键盘按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31784288/

相关文章:

ios - CTFontManagerRegisterFontsForURL 在设备上不起作用

ios - 什么是 NSManagedObjectContext 的 performBlock : used for?

ios - 为 iOS 企业分发创建自己的 AppStore

ios - XCode Generic Archive 而不是 iOS app Archive

ios - 如何为 UITextView Swift 添加下划线/粗体/斜体能力

ios - Swift ISO8601 格式到 Date 返回 fatal error

ios - 在散点图上绘制置信椭圆

swift - 如何确定 Swift 中滑动的速度和角度

objective-c - 在 Swift 中创建 NSDateFormatter?

xcode - 在 Xcode 中使用 embedXcode 进行 Arduino 开发