I'm new on swift so I'm having problems for coding. I would like to add an action to the return button on keyboard, so that when I type it the keyboard would hide and the scene (the view) would change. So how can do it?
Asked
Active
Viewed 706 times
-3
-
possible duplicate of [how to add an action on UITextField return key?](http://stackoverflow.com/questions/11553396/how-to-add-an-action-on-uitextfield-return-key) – Stuart Mar 17 '15 at 11:23
-
[This question](http://stackoverflow.com/a/11553533/429427) discusses how to respond to `UITextField`'s return key, and [this documentation](https://developer.apple.com/library/ios/recipes/xcode_help-IB_storyboard/chapters/StoryboardSegue.html) describes how to use segues to transition between storyboard scenes. – Stuart Mar 17 '15 at 11:27
-
That's don't solve my problem cause it doesn't change scenes – dp6000 Mar 17 '15 at 12:10
-
The second link I provided describes how to change scenes in storyboards (or programmatically) using segues. – Stuart Mar 17 '15 at 12:37
-
But it doesn't show how to change view pressing the return button in the keyboard. – dp6000 Mar 17 '15 at 13:29
-
No, the first link describes how to respond to the return key. Essentially, you need to implement `UITextFieldDelegate`'s `textFieldShouldReturn(_:)` method, and from within that method you will call `performSegueWithIdentifier("NextSceneIdentifier")` (if you have such a segue setup in your storyboard) or instantiate a new view controller and push to it using `presentViewController(_:animated:completion:)` (for a modal push) or `navigationController.pushViewController(_:animated:)` (for pushing onto the current nav stack). – Stuart Mar 17 '15 at 14:14
-
My code is like this, and it isn't working: func textFieldShouldReturn(textField: UITextField!) -> Bool { //delegate method textField.resignFirstResponder() if(textField == self.myTextField) { textField.resignFirstResponder() performSegueWithIdentifier("InitialView", sender: nil) return false } return true; } – dp6000 Mar 17 '15 at 16:12
-
The ViewDidLoad is like this: override func viewDidLoad() { super.viewDidLoad() self.myTextField.delegate = self } – dp6000 Mar 17 '15 at 16:16
-
I suggest you edit your question with more specific details of the problem - as it is, it is unlikely to be answered and will probably be closed because it is very broad. Be clear about any errors or unexpected behaviour, indicate what you expect should happen, and include relevant code or steps to reproduce the problem. – Stuart Mar 17 '15 at 16:24
-
Sorry! My app run successfully ,my problem is that when I press the return button on keyboard the app close. I'm doing something wrong in my code and I don't know what. The "InitialView" identifier is the identifier of a segue that connect the textField with a ViewController, with a push. – dp6000 Mar 17 '15 at 16:30
-
If you follow my suggestion of editing your question with more specific details (by clicking "edit" above these comments), it may be salvaged and attract a response. For help writing a good question and improve your chances of getting an answer, see this section of the Help Center: [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – Stuart Mar 19 '15 at 15:39