2015-08-03 19 views

回答

4

您可以使用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 
    } 
}