我想知道如果用户触碰了UITextField之外的键盘,如何关闭它。我正在使用Xcode 6.1。我按照下面的thruUITextField函数将aUIViewController添加到aViewDidLoad()中。如果你能帮助我们解除董事会的职务,我们将不胜感激。

    class PickerdemoViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate{

var textBox1 : UITextField!

override func viewDidLoad() {

//..................adding text box.........................//

    self.textBox1 = UITextField (frame: CGRectMake(100, 152.5, 50, 35))
    textBox1.delegate = self
    textBox1.backgroundColor = UIColor.whiteColor()
    textBox1.placeholder = "enter"
    textBox1.keyboardType = UIKeyboardType.DecimalPad
    self.textBox1.resignFirstResponder()
    textBox1.keyboardAppearance = UIKeyboardAppearance.Default
    self.view.addSubview(textBox1)



    super.viewDidLoad()

}

最佳答案

您需要对UITextField有一个引用,所以请创建这样的属性值

class MyClass: UIViewController {
  var textBox1: UITextField!
  ...
  // create your textfield where ever you were by assigning it to self.textBox1
}

然后要关闭键盘,你就要辞去它的第一响应者的职务。
self.textBox1.resignFirstResponder()

更新到dimiss键盘
返回时退出/使用textField委托方法完成
func textFieldShouldReturn(textField: UITextField) -> Bool {
    self.textBox1.resignFirstResponder()
    return true
}

单击按钮时退出(自定义IBAction方法)
@IBAction func buttonClicked(sender: UIButton!) {
    self.textBox1.resignFirstResponder()
}

关于swift - 快速解散键盘,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26993488/

10-09 16:18
查看更多