Xcode 7的键盘控制功能错误

时间:2015-10-27 19:36:46

标签: ios iphone swift xcode7

我试图创建一个功能,以便通过点击键盘外部或键盘内的返回键来放置键盘,但遗憾的是,只有当我在键盘外面单击时它才起作用,它无法按下键盘中的返回键。

import UIKit

class ViewController: UIViewController,UITextFieldDelegate {

@IBOutlet var numberEnter: UITextField!


@IBAction func findButton(sender: AnyObject) {

    resultLabel.text = numberEnter.text
}


@IBOutlet var resultLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    self.view.endEditing(true)
}

func textFieldShouldReturn(textField:UITextField) -> Bool{

textField.resignFirstResponder()

return true

}

}

1 个答案:

答案 0 :(得分:0)

如果您没有为文本字段设置代理,则需要执行此操作。您可以在Interface Builder或代码中设置委托。请参阅此StackOverflow答案以获取示例:Text Field Should Return, is this correct for ios7?