(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.)
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
}
}
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
}
I'm writing an app for iOS where I have 3 values, and every value is the result of two the others two. So, if you compile two of the three you have the third value.
Right now I have to clear the value inside the Texfield manually before calculate the third value otherwhise the app dont work. What I want to do is that when I compile the first and the second textfield the third clear automatically.
In practise I want to check the last two entered textfield and clear the other.
How can I check this statement in swift?
Thank you
try to use TextFieldDelegate for this, in the case you are training to display this may help
class ViewController: UIViewController,UITextFieldDelegate //set delegate to class
#IBOutlet var TF1: UITextField
#IBOutlet var TF2: UITextField
#IBOutlet var TF3: UITextField
override func viewDidLoad() {
super.viewDidLoad()
TF1.delegate = self //set delegate to textfile
TF2.delegate = self //set delegate to textfile
TF3.delegate = self //set delegate to textfile
}
func textFieldDidBeginEditing(textField: UITextField!) { //delegate method
}
func textFieldShouldEndEditing(textField: UITextField!) -> Bool { //delegate method
return false
}
func textFieldShouldReturn(textField: UITextField!) -> Bool { //delegate method
textField.resignFirstResponder()
return true
}
func textFieldDidBeginEditing(textField: UITextField) {
if(TF1.text != "" && TF2.text != ""){
TF3.text = ""
}
}
I hope this can help you
I have a textField in my TableViewController with a search return key.
I succeeded to put some text programmatically in the search textField but I didn't succueed to run the search option programmatically.
This is my code so far:
When I try to run the last row (textFieldShouldReturn(search_textfield)), the application crashes.
How can I call programmatically to textFieldShouldReturn in order to activate the "Search" button?
if i understood your question correctly you want the following:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
}
the textFieldShouldReturn function gets called when you hit the return key on the keyboard
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.resignFirstResponder() //to hide the keyboard
//call any of your functions
callFunction()
return true
}
func callFunction() {
print("called function after return key was pressed...")
}
All this works perfectly for me in Xcode8 with swift3 and ios10.
As from your code it looks like you want to search the default value that is written in your textfield that why you write textfieldShould Return..
you can do this by creating a method like
-(void)PerformSearch:(NSString*)searchStr
{
//Do what ever Logic you implement in searching
}
Call this method in your ViewDidLoadMethod to perform Search
-(void)ViewDidLoad
{
[super:ViewDidLoad];
[self performSearch:textfield.text];
}
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;
}
}