Skip to content

Instantly share code, notes, and snippets.

@sigwyg
Created February 24, 2016 07:19
Show Gist options
  • Save sigwyg/304f5ca96d1f60fe2c23 to your computer and use it in GitHub Desktop.
Save sigwyg/304f5ca96d1f60fe2c23 to your computer and use it in GitHub Desktop.
Swiftでキーボードを制御するサンプル。UITextFieldDelegateを忘れないように。
class SecondViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var item: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// viewControllerがtextFieldに対するDelegate通信先であることを示す
self.item.delegate = self
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
// ユーザがキーボード以外の場所をタップすると、キーボードを閉じる
self.view.endEditing(true)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
// return を押すと、キーボードを閉じる
textField.resignFirstResponder()
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment