keyboard done key action swift iOS doesn't work - ios

I'm new in stackoverflow, I have a problem with new swift code.
I have custom the return button on keyboard with "Done", but when I tap on it, don't befall anything... How can I hide the keyboard on tap it?
I have added a code (found on this site) for hide the keyboard when you tap somewhere ,not in the keyboard, but I can't custom it with tap on "done" button... Thank you before!!

You need to implement delegate method which is called when you hit done button:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
You also need to conform to UITextFieldDelegate protocol:
// I assume you override UIViewController class. If not add UITextFieldDelegate to your class
class MyViewController: UIViewController, UITextFieldDelegate
The last thing is set up your class to be a text field delegate:
textField.delegate = self

textField.delegate = self
can be replaced by
This will create the necessary connections between your View, its component and will make the textFieldShouldReturn method work as expected.

The protocol methods have new signatures (Swift 4.1). IE:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
As the protocol methods are optional, using a wrong signature will silently fail.

Related

How to add countdown-timer to UIAlertController

I'm quite new to Swift and couldn't find any good explanation for this. I want the user to select a duration (h:m:s) when clicking on a UITextField in an alert and display the value in the textField.
Can someone please give me a hint or an explanation.
UITextField has delegate protocol called UITextFieldDelegate
in your viewController:
func viewDidLoad() {
super.viewDidLoad()
yourTextField.delegate = self
}
Conform your viewController to this delegate. There is a textFieldShouldBeginEditing method, it is called when the user taps into the textfield. You return false here, so the textField won't became first responder and the keyboard will not open and you can present your picker.
extension viewController: UITextFieldDelegate {
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
//open your alert here
openPicker()
return false
}
}
You can use UIPickerView to present to the user. You can read about that here and even create it based on the example:
explanation and example of UIPickerView

Make UITextField clear content each time return key pressed

I'm learning Swift by making a times tables app, which simply creates random times tables and asks the user to type the answer in a UITextField.
I'd like the user to be able to tap the return key and have the text field clear their previous answer and have the keyboard remain in view. I've already set up the other behaviour I want, it's just I can't seem to find any similar questions to figure out how to clear, and do all this when there return key is tapped.
Here's the code I have so far, which works when dismissing the keyboard, and doesn't clear the field.
Many thanks!
#IBAction func answerTyped(_ sender: UITextField) {
/* Clear the text field*/
questionNumber += 1
attempted += 1
markQuestion(answer: answer)
newQuestion(awardLevel: currentLevel, questionNumber: questionNumber)
}
If you want the textField to be cleared on tapping return button you can use the textFieldShouldReturn delegate.
func textFieldShouldReturn(_ textField: UITextField) -> Bool { //delegate method
textField.text = "" // Clears text
//Do other things that you want to do when user taps return button
return true
}
Note that this wont dismiss the textField like how you asked and usually users are used to dismissing their textField using return. So if you ask me this is not good UX. And you need to have a mechanism for the user to dismiss the keyboard if needed like tap outside to dismiss or something. So i hope you have that sorted out.
Your viewController should have implemented the UITextFieldDelegate for this method work.
class YourViewController: UITextFieldDelegate {
var textField: UITextField! //Using IBOutlet or whatever
func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
}
}
you will need to make something, likely your view controller containing this text field into a uitextfieldelegate, then add the necessary delegate functions.
I think the function you want is TextField:DidEndEditing:Reason https://developer.apple.com/documentation/uikit/uitextfielddelegate/2352220-textfielddidendediting
Or you can just use
func textFieldShouldReturn(_ textField: UITextField) -> Bool { //delegate method
textField.resignFirstResponder()
return true
}
which is called every time user hits the return key. You would need to add your logic to clear the contents before the returns.

How to prevent editing of text in a UITextField without disabling other events using Swift 3?

I'm trying to modify the text rendered in the UITextField based on certain events such as Touch Down or Touch Down Repeat. I want the UITextField to be responsive only to events but prevent users from modifying the actual value in the UITextField.
I have tried unchecking the Enabled state of the UITextField but that causes it to not respond to touch events either.
How do I prevent users from changing the contents of the UITextField without affecting the response to touch events, specifically using Swift 3?
So, I was finally able to get this working in the expected manner. What I did was -
1 - Extend the UITextFieldDelegate in the ViewController class
class ViewController: UIViewController, UITextFieldDelegate {
2 - Inside the function viewDidLoad(), assign the textfield delegate to the controller
override func viewDidLoad() {
super.viewDidLoad()
textfield.delegate = self
}
(you will have assign each UITextField's delegate that you want to be prevented from editing)
3 - Lastly, implement your custom behavior in the textFieldShouldBeginEditing function
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return false
}
For reference, check out this API Reference on Apple Developer
Override textFieldShouldBeginEditing , and return false.
func textFieldShouldBeginEditing(state: UITextField) -> Bool {
return false
}
you can also disable for specific text field.
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
if(textField == self.myTextField){
return false
}
return true;
}

Swift - Text Field Keyboard Return Key Not Working?

I am fairly new to swift and was working on a project where I included a text field. When I tested out the app, I noticed that the keyboard return key doesn't work and I am unable to exit the keyboard. Is there a reason why it does this? Can the return key's functionality be implemented through swift?
Also, there is another app I was recently working on that I switched the keyboard of a text field to number pad, and I realize there is no return key on the number pad, so again, I can't seem to figure out how to exit it. How could I fix this?
Sorry, I am pretty new to apple devices as well...
You want to implement UITextFieldDelegate in your view controller:
class ViewController: UIViewController,UITextFieldDelegate //set delegate to class
and then in viewDidLoad set the textField delegate to self.
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
}
You can then add this method which runs when the return key is pressed, and resignFirstResponder which closes the keyboard:
func textFieldShouldReturn(textField: UITextField!) -> Bool { //delegate method
textField.resignFirstResponder()
return true
}
For other newbies like myself, if you still don't have a keyboard on the simulator after following Joe's excellent instructions then go to the Simulator's menu and select Hardware/Keyboard/Toggle Software Keyboard.
Please note that in Swift 4 it seems that the first parameter name of textFieldShouldReturn has to be omitted with an underscore. Elsewise Swift does not call the function:
func textFieldShouldReturn(_ textField: UITextField!) -> Bool { // use "_"
return true;
}

Hide keyboard on tap on each view - Swift

I'm not going to ask how to hide the keyboard after you are done with editing a textField. My question is : Is there a way to do this on each view ? (like a setting) or do I need to write the two following functions and set the delegate properly every time ?
func textFieldShouldReturn(textField: UITextField) -> Bool // called when 'return' key pressed. return NO to ignore.
{
textField.resignFirstResponder()
return true;
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
I'm developing an app with a lot of textfields (and also views) so I try to avoid redundance code. What is the best solution to avoid this repetition?
Thank you!
You can create your own text field, which is subclass of UITextField. See the simple custom UITextField below:
import UIKit
class CustomTextField: UITextField, UITextFieldDelegate {
override func awakeFromNib() {
super.awakeFromNib()
self.delegate = self
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
self.resignFirstResponder()
return true
}
}
Use your custom text field name to Custom Class in your Storyboard.
See my example at Github
The easiest thing to do, is put one giant invisible button the size of the screen underneath your text fields, then when a non text field is tapped, you call the invisible button action to close it. If this does not apply in your scenario please let me know.
Create an IBAction method to dismiss keyboard
#IBAction func backgroundTapped (sender: UIView)
{
sender.endEditing(true)
}
Change the class of your UIView to UIControl which contains the textfields in storyboard (You can even do that to your view of the viewcontroller as shown)
It looks like this:
Now you can connect the IBAction to the Touch Up Inside event of this view, in the storyboard, as shown.

Resources