I want my UITextField to be editable from picker view but not selectable (I do not want copy,paste etc. options to be displayed). I achieved that it is editable from picker view but it is also selectable, which I don't want.
Is there a way to do so?
Thanks :)
set timeTextField.delegate = self
extension YourViewController: UITextFieldDelegate {
// For not allow user edit or cut
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
textField.isUserInteractionEnabled = false;
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
textField.isUserInteractionEnabled = true;
}
}
Related
Currently I have a class that conforms to UITextFieldDelegate. In this class I have two textfields: Password and Phone-Number. In addition, I also have two methods:
1:func textFieldShouldBeginEditing(UITextField) -> Bool
2:func textFieldDidEndEditing(UITextField, reason: UITextField.DidEndEditingReason)
How do I associate my password textfield with the first function but not the other and vice versa? Essentially, when I set the textfields delegate, I want to be able to choose which methods gets called for each respected textfield?
You can check if its the field you want to use i delegate function
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField == Password {
return true
}
return false
}
func textFieldDidEndEditing(_ textField: UITextField) {
if textField == phoneNumber {
// do something
}
}
I'm managing user actions in UITextField with func textFieldShouldReturn(_ textField: UITextField) -> Bool and it works perfectly but if I set textField.text = "String"
This is not being called, I also tried with IBActions like valueChanged and DidEndEditing with no success
Any idea how can I solve this?
The delegate methods are not called when you change the text property programmatically.
But when you change the property in code you know that and when the value changed.
For example write a common function
func updateTextField(with string: String) {
textField.text = string
doSomethingAfterTextChanged()
}
Or use a property observer which is called when the value of textFieldValue changed
var textFieldValue : String {
didSet {
textField.text = textFieldValue
doSomethingAfterTextChanged()
}
}
You can do that. Add this code in your viewDidLoad
yourTextField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: UIControl.Event.editingChanged);
Add this function in your viewController
#objc func textFieldDidChange(_ textField: UITextField)
{
// you are done check your value do something
}
Below is my code for hiding keyboard on pressing return key, But it's not working.
class AddHall: UIViewController,UITextFieldDelegate {
#IBOutlet weak var hallname: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
hallname.delegate = self
}
func textFieldShouldReturn(hallname : UITextField!) -> Bool {
hallname.resignFirstResponder()
return true
}
}
Implement correct UITextField Delegate method.
replace
func textFieldShouldReturn(hallname : UITextField!) -> Bool {
hallname.resignFirstResponder()
return true
}
with
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
The delegate method textFieldShouldReturn is used to specify if the text field is allowed to lose the focus - it will only be called just before the UITextField is about to lose its focus. You should only do some checks her, but not dismiss anything.
What you seek is to react on the return key, and then dismiss the keyboard. This is done by connecting the DidEndOnExit action (be aware: there are a lot of other events with similar names, you'll have to exactly use this one), and there resign the first responder.
You can then just remove textFieldShouldReturn (unless you do some additional checks here and not simply return true).
change your code like this. You are not using correct delegate method.
func textFieldShouldReturn(textField : UITextField!) -> Bool {
textField.resignFirstResponder()
return true
}
You have to use correct function name for TextField Delegate
Use this:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
It is real common issue with hiding keyboard in iOs app. To solve it i use
class myViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var myTextField: UITextField!
and after that
override func viewDidLoad() {
super.viewDidLoad()
self.myTextField.delegate = self;
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
But it is not working this time because i have Editing Changed event of myTextField linked to one of my functions. So keyboard is not hiding.
How it can be solved in this case?
Calling textFieldShouldReturn do nothing special to hide keyboard. You have to hide it yourself.
func textFieldShouldReturn(textField: UITextField) -> Bool {
return textField.resignFirstResponder()
}
it is not working this time because i have Editing Changed event of myTextField linked to one of my functions
Normally what you have done should still work when return is clicked, I think. But perhaps there is some other factor I am missing. Perhaps related to something that the function does.
Here is an alternative way of dismissing the keyboard. Created with ctrl-click drag from text field's Did End On Exit
#IBAction func textFieldDoneEditing(sender: UITextField) {
sender.resignFirstResponder()
}
Hope this helps you:
func textFieldShouldReturn(textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
//do not forget to call textfield delegate in viewdidload
override func viewDidLoad() {
super.viewDidLoad()
self.myTextField.delegate = self;
}
Thank you for your answers - unfortunately they did not solve my problem.
I tried -resignFirstResponder with no success until i added return button to keyboard. Now after pressing it, keyboard hides.
(iOS8, Xcode6, Swift)
Using Swift, how do I capture a tap on the "Return" button?
The doc at the following link specifies using the textFieldShouldReturn method:
// Swift
#optional func textFieldShouldReturn(_ textField: UITextField!) -> Bool
Where I'm hung up is in the "_ textField" part. I've created the text field using Storyboard. How do I capture notifications for this specific text field? Do I need to create a new class and set it as a delegate for this text field? Do I assign the text filed a name, and then somehow hook into it?
https://developer.apple.com/documentation/uikit/uitextfielddelegate/1619603-textfieldshouldreturn
class ViewController: UIViewController,UITextFieldDelegate //set delegate to class
#IBOutlet var txtValue: UITextField //create a textfile variable
override func viewDidLoad() {
super.viewDidLoad()
txtValue.delegate = self //set delegate to textfile
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool { //delegate method
textField.resignFirstResponder()
return true
}
Implement this function
func textFieldShouldReturn(_ textField: UITextField) -> Bool { //delegate method
textField.resignFirstResponder()
return true
}
And for delegate you can set using the Utilities pane / Connections Inspector / delegate and then drag to ViewController (yellow button in the storyboard)
Then you do not need to set the delegate programmatically for every text field
In Swift 4.2 and Xcode 10.1
//UITextField delegate method
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == TF1 {
textField.resignFirstResponder()//
TF2.becomeFirstResponder()//TF2 will respond immediately after TF1 resign.
} else if textField == TF2 {
textField.resignFirstResponder()
TF3.becomeFirstResponder()//TF3 will respond first
} else if textField == TF3 {
textField.resignFirstResponder()
}
return true
}
You need to set an object as the text field's delegate. Usually that would be the view controller that the text field exists in. You don't need to inherit from any other class, or, strictly speaking, implement a delegate (but you could implement UITextFieldDelegate to make things a little clearer.)