Created
February 24, 2016 07:19
-
-
Save sigwyg/304f5ca96d1f60fe2c23 to your computer and use it in GitHub Desktop.
Swiftでキーボードを制御するサンプル。UITextFieldDelegateを忘れないように。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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