what's the code meaning? UITextFieldDelegate - ios

How does this code work -
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField.resignFirstResponder() {
textField.text = nil
}
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
theTextField.text = textField.text
}
I don't understand it, I know what it does but i need some help to understand what the codes mean. Thank you! :)

ShouldReturn have to be called earlier than DidEndEditing, if ShouldReturn
ends with true. TextField is a first responder, therefore textField.text and theTextField.text are nil.
I could assume that original idea is to copy value from textField to theTextField, and nullify the first field.
ShouldReturn is called to figure out if editing is finished, and resign method will deactivate currently active textField.
Then after that DidEndEditing(_ textField is going to be called for the first(resigned) textField, but the text is already nil(was reset), that is thy theTextField.text is going to be nil.
Please check the UITextFieldDelegate

Related

Is there a way to hide a UILabel when the user starts typing, and then make it re-appear if the user removes all input?

I'm trying to implement a search field where you type in some characters to show "possible searches". In the beginning there is a small text underneath the textbox that says "make sure to capitalize letters". This is just a UILabel. I want to make this label "disappear" when the user STARTS to type. But if the user backspaces enough to remove all text - I want the label to re-appear again.
The textbox is just made from UITextField.
This is all stored in a view.
Does anyone have an idea of how to implement something like this?
You Can do it using add a target to your textFiled with a selector like this
add this target to ur viewDidLoad method
yourTextFiled.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: UIControl.Event.editingChanged)
create selector method like this below your viewDidLoad Method and set if textfield.text.isEmpty than label is hidden like this
#objc func textFieldDidChange(_ textField: UITextField) {
if textField.text!.isEmpty {
label.isHidden = false
} else {
label.isHidden = true
}
}
Hope you get your desire thing !
You can do this by the textfield delegate method shouldChangeInCharacter and check when textfield is empty set the label isHidden property to false otherwise too true.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let text = textField.text {
if text.isEmpty {
label.isHidden = false
}
else {
label.isHidden = true
}
}
return true
}
Hope this will help you. Happy coding!

Clear button on UITextField

I have a UITextField with date picker, and I want to use the default built in button to clear the text field, it doesn't seems to work.
I tried overriding
func textFieldShouldClear(_ textField: UITextField) -> Bool {
searchBar.text = "" //my textfield is called searchBar and I tried to clear the text like that
return true
}
It seems that the method is called and printing the textfield says its empty but its not and in the view shows the last date I entered. Any ideas? Thanks in advance.
You are searching this:
textfield.clearButtonMode
Have you tried to replace searchBar with textField (local variable) in the function like this?
func textFieldShouldClear(_ textField: UITextField) -> Bool {
textField.text = "" // use local variable textfield here
return true
}
I found out that clear button is updating the text field and still takes the date from the date picker... Now I am trying to clear the value of the date picker maybe this will work.

Swift 4 - identifying only one text field as delegate for editingDidEnd etc

I've kept this question free of specific context because I'm sure it will be helpful for others :
I have 2 IB outlet text fields :
#IBOutlet weak var textField1: UITextField!
#IBOutlet weak var textField2: UITextField!
I want to be able to disable buttons on my page until both have been filled out properly so I'm using the following delegates in ViewDidLoad() (I have added TextFieldDelegate to my VC.)
override func viewDidLoad() {
textField1.delegate = self
textField2.delegate = self
}
I then have some functions I will use to do the form validation and to take specific actions but to keep it simple let's say it simply prints to the console.
What I want to do is only check for validation in textField1 and not in textField2. I.e. the desired output is that this prints for when user begins editing textField1 but if user edits textField2 nothing is printed.
I'm currently using :
func textFieldDidBeginEditing(_ textField1: UITextField) {
print("TextField did begin editing method called")
}
But that is printing when either textField is edited.
I thought I've specified _ textField1 so not sure why both are triggering it?
There are some answers solving similar problems for Swift 3 and earlier. In particular one answer referenced this link http://sourcefreeze.com/uitextfield-and-uitextfield-delegate-in-swift/ which i've found useful but am stuck on this error.
UITextField delegate method allows you to identify that which textField is begin editing so you just need to check whether it's your 1st textField or not like this.
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == self.textField1 {
print("TextField did begin editing method called")
// Do your Validate for first text field
} else {
//Do Nothing
}
}
That's why the delegate function textFieldDidBeginEditing has a textField parameter. Renaming it does nothing.
If you want to discern your text fields, compare the textField parameter to your IBOutlets, like so:
func textFieldDidBeginEditing(_ text Field: UITextField) {
if textField == textField1 {
// Validate first text field
} else if textField == textField2 {
// Validate second text field
}
}
simply set tag for both textFields as 0 and 1 then in your delegate method just check the tag of your textfield.
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField.tag == 0 {
print("TextField did begin editing method called for text field 1")
}
}
Let's first analyse your piece of code and see what is wrong:
func textFieldDidBeginEditing(_ textField1: UITextField) {
print("TextField did begin editing method called")
}
You renamed textField to textField1. However, that does not name the UI element that should be responsible for this action. Instead, that is a function parameter which lets you access information about the object that called the action from within your function, regardless of the outer scope. Instead, you should use the === operator, which checks whether two references point to the same object instance. So your code should become:
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField === self.textField1 {
print("TextField 1 did begin editing method called")
}
}
You have named the parameter that receives the UITextField reference that is passed to the delegate method textField1; the name of this parameter is nothing to do with the name of your property. You could have called it textField and your code would still compile, despite not having a property called textField.
You need to compare the instance of UITextField that was passed to the delegate method:
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField === self.textField1 {
print("TextField did begin editing method called for text field 1")
}
}
you can do both ways either you can put if condition on textfield like paul said or if you don't want textfield2 to work with any of the textfield delegate you can remove the line
textField2.delegate = self

what function can help me to know user input first character in textfield and how to make a blank line when I press keyboard return?

I have two problems.I am a beginner of swift
First,I want to hide the send button until user input frist character.I use textfield delegate functions that they don't implement this idea.
And then, I want to creat a blank line when user input in textfield.I know to use "textFieldShouldReturn" function ,but I use "\n" that it doesn't work for me.How to do this?
this is my code:
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField.text?.isEmpty == false {
aButton.isHidden = true
sendButton.isHidden = false
}
}
update code:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.textField.text = "\n"
return true
}
I also want the textfield change it height.let user can input their message. Does it can be implement?
Or I need to change textfield to textview.
You can use textfields delegate method to check when user start writing something in your textfield.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
{
if textField == txtUserName //compare with your textfield object which you have taken by outlet
{
if string.characters.count >= 1 {
//make your button enable
}else
{
// disable your button
}
}
return true
}

UITextField - delegate for clearButtonMode?

I want the X button to show only when
textField.clearButtonMode = .UnlessEditing
When the user pushes the X button, the textView becomes the first responder, and the keyboard pops up.
What do I do so that when the X button is pushed, the text only clears, but the textView does not become focused?
- (BOOL)textFieldShouldClear:(UITextField *)textField
this method is your want,you can check something in it.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/index.html#//apple_ref/doc/uid/TP40006991-CH3-SW10
i hope above link can help you.
If you want it to be focus and able to key in, try
textField.becomeFirstResponder()
If you mean you want it not to show the keyboard and just clear the text, you can delegate your UITextField and use the function to return false:
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
textField.text = ""
return false
}
You can implement this delegate like the following:
func textFieldShouldClear(_ textField: UITextField) -> Bool {
textField.text = ""
return false
}

Resources