I have the following code:
class ViewController: UIViewController, UITextFieldDelegate {
// MARK: Properties
#IBOutlet weak var layersTextField: UITextField!
#IBOutlet weak var innerShapeTextField: UITextField!
#IBOutlet weak var outerShapeTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
layersTextField.delegate = self
innerShapeTextField.delegate = self
outerShapeTextField.delegate = self
}
// MARK: UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(textField: UITextField) {
// do something
}
}
Now in textFieldDidEndEditing(_:) I would like to do something, dependent on which UITextField called this method.
Is there any way to distinguish, which UITextField did this? Is there some kind of ID or identifier I can set on the UITextFields?
You can make this determination using one of two approaches: outlets or tags. For the outlet approach, declare an outlet instance variable (using the IBOutlet keyword) and then make an outlet connection. In your delegation method, test whether the passed-in text object is the same object referenced by the outlet, using pointer comparison.
For example, say you declare and connect an outlet named SSN. Your code might look something like Listing 3-1:
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
if (textField == SSN) {
// ...
return NO;
}
return YES;
}
// Translated to Swift:
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
if textField === SSN {
// ...
return false
}
return true
}
You can check by the name of the next filed. if textField == layersTextField { //do what you want } and you can do that for any text field you need a specific action for.
You can create an IBAction instead:
Related
I am creating a signup screen.
There are four UITextField, which is the ID, password, password check, name,
I am #Iboutlet var signupTextFields: [UITextField]! I connected it. After that, I want to make an ID, password validation.
First, I divided ViewController and UItextFieldDelegate.
class SignUpViewController: UIViewController {
#IBOutlet var signUpTextFields: [UITextField]! {
didSet {
signUpTextFields.forEach { textField in
textField.delegate = textFieldDelegate
textField.returnKeyType = .next
}
}
}
#IBOutlet weak var nextButton: UIButton!
private lazy var textFieldDelegate = TextFieldDelegate(self)
override func viewDidLoad() {
super.viewDidLoad()
signUpTextFields.first?.becomeFirstResponder()
}
#IBAction func nextButtonTapped(_ sender: UIButton) {
}
}
class TextFieldDelegate: NSObject, UITextFieldDelegate {
private weak var signUpViewController: SignUpViewController?
init(_ signUpViewController: SignUpViewController) {
self.signUpViewController = signUpViewController
}
func textFieldDidBeginEditing(_ textField: UITextField) {
textField.layer.borderWidth = 1
textField.layer.borderColor = UIColor.systemBlue.cgColor
}
func textFieldDidEndEditing(_ textField: UITextField, reason: UITextField.DidEndEditingReason) {
textField.layer.borderColor = UIColor.black.cgColor
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
The problem when using the functions provided by Delegate is that the example code is to identify UITextField using if-else, such as
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == idTextField {
//code
} else if textField == passwordTextField {
//code
}
}
I was not this way, but I thought I wanted to abstract the uitextfield a little more to use the Factory method or polymorphism.
Is there a way to identify a UITextField Collection without using if-Else?
I'd recommend - Tag + Enum. (tag field is the norm to identify a view instance from Storyboard / Xib in the cases where you don't have individual IBOutlets)
Set "tag" in the Storyboard - ensure they are unique. (simple index oughta be enough -- 0, 1, 2 .. etc.)
Create an enum inside the view controller.
enum TextField {
case name = 0
case password = 1
}
& so on (explicitly declare the tag values in the enum)
Use Switch instead of if-else for identification.
switch textField.tag {
case .name:
case .password:
}
This should suffice for your case (since you are just going through an example code). But it's good that you are interested in clean code -- I'd recommend creating the TextFields programmatically & using the enum to set the .tag field.
Using the same delegate for all of the text fields was bad design if your intention was to behave differently in the delegate methods. If it’s too late to change that, I would recommend an array of closures corresponding to the array of text fields. That way, calling the right closure is a one-liner based on firstIndex(of:).
How do I find out if the keyboard is of type numeric, Twitter, email, etc...?
edit: Is there a way to detect keyboard type without using an outlet?
Consider that you have tow textFields in the ViewController, You will need to implement textFieldShouldBeginEditing method from UITextFieldDelegate protocol, as follows:
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var tfEmail: UITextField!
#IBOutlet weak var tfPassword: UITextField!
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField.keyboardType == .emailAddress {
// this is the tfEmail!
}
if textField.isSecureTextEntry {
// this is tfPassword!
}
}
}
Make sure their delegates are connected to the ViewController, programmatically:
tfEmail.delegate = self
tfPassword.delegate = self
or from the Interface Builder.
Note that you can recognize the keyboard type for the current textField by checking its keyboardType property, which is an instance of UIKeyboardType enum:
The type of keyboard to display for a given text-based view. Used with
the keyboardType property.
What about UITextView?
The same exact functionality should be applied when working with UITextViews, but you need to implement textViewDidBeginEditing(_:) method from UITextViewDelegate protocol instead of implementing textFieldShouldBeginEditing. Again, make sure the delegate of the textView is connected to the ViewController.
Also,
If your main purpose of checking the keyboard type is just for recognizing what is the current responded textField/textView, I suggest to do a direct check:
class ViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate {
#IBOutlet weak var tfEmail: UITextField!
#IBOutlet weak var tfPassword: UITextField!
#IBOutlet weak var textViewDescription: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
tfEmail.delegate = self
tfPassword.delegate = self
textViewDescription.delegate = self
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField === tfEmail {
// this is the tfEmail!
}
if textField === tfPassword {
// this is tfPassword!
}
}
func textViewDidBeginEditing(_ textView: UITextView) {
if textView === textViewDescription {
// this is description textview
}
}
}
For more information about === operator you might want to check this question/answers.
Hope this helped.
In addition to Ahmad F 's great answer, this is my approach of getting the current keyboard type, at any time:
Step 1: Delegate UITextField
class File: UIViewController, UITextFieldDelegate{//...}
Update viewDidLoad() to this:
#IBOutlet weak var normalTextField: UITextField!
#IBOutlet weak var numberTextField: UITextField!
#IBOutlet weak var emailTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
numberTextField.keyboardType = .numberPad
normalTextField.keyboardType = .default
emailTextField.keyboardType = .emailAddress
numberTextField.delegate = self
normalTextField.delegate = self
emailTextField.delegate = self
}
Step 2: Working with UITextField's methods:
Add a variable called keyboardType, as below:
var keyboardType: UIKeyboardType? = nil
Then, change it whenever a new textField begins editing:
func textFieldDidBeginEditing(_ textField: UITextField) {
keyboardType = textField.keyboardType
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
keyboardType = nil
return true
}
Step 3: Create and call a function like below:
func getCurrentKeyboard() -> String{
if keyboardType == nil{
return "no current keyboard"
}
else if keyboardType == .numberPad{
return "number"
}
else if keyboardType == .emailAddress{
return "email"
}
else{
return "default"
}
}
#IBAction func displayCurrentKeyboard(_ sender: UIButton) {
print(self.getCurrentKeyboard())
}
And this outputs: email / number / no current keyboard / default, depending on the case.
If you want to check which type of keyboard it is with if-else statements, you can change your displayCurrentKeyboard() method to this:
#IBAction func displayCurrentKeyboard(_ sender: UIButton) {
let keyboardString = self.getCurrentKeyboard()
if keyboardString == "number"{
//...
}
else if keyboardString == "email"{
//...
}
else{
//...
}
}
And that's it! You can call this wherever you want in your code with this usage:
let keyboardString = self.getCurrentKeyboard()
NOTE: This method also handles the case of no keyboard visible on the screen, returning no current keyboard, in this case.
Let me know if this helps!
I am not sure why textFieldShouldBeginEditing returns all of the UiTextField
File: PaymentViewControllerDummy.swift
class PaymentViewControllerDummy: UIViewController, UITextFieldDelegate {
#IBOutlet weak var dobTextField: UITextField!
#IBOutlet weak var nameTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
dobTextField.tag = 1
nameTextField.tag = 2
dobTextField.delegate = self
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
println("Tage From textFile: \(textField.tag) ")
println("Tage From dobTextField: \(dobTextField.tag) ")
if(textField.tag == dobTextField.tag) {
println("You are editing date of birth")
return false
} else {
return true
}
}
}
All the IBOutlets are connected. This is very standard code. I have done this times after time however whichever textfield I press textField == self.dobTextField comes back true
Console result:
Am I missing something?
Edit
Here is the Interface builder screens
NOTE
I made a standalone project and copy the codes to the project and it worked as it should however it is not working in this project. Could it be something in the StoryBoard ?
You shouldn't check UI objects with equality comparison.
You had better use tags (or maybe labels, not preferred however) in order to conduct protocol calls over different UI objects.
class PaymentViewControllerDummy: UIViewController, UITextFieldDelegate {
#IBOutlet weak var dobTextField: UITextField!
#IBOutlet weak var nameTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
dobTextField.delegate = self
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
println(textField.tag)
println(dobTextField.tag)
if(textField.tag == dobTextField.tag) {
println("You are editing date of birth")
return false
} else {
return true
}
}
}
Also you DON'T have to refer self if you want to access instance variables, if you are not in a block (closure).
The best thing to access text fields in text field did editing method is via tags. Set the tag of both the text fields let say self.dobTextField.tag=1 & self.nameTextField.tag=2. Then in
func textFieldShouldBeginEditing(textField: UITextField) -> Bool
println(textField.tag)
println(dobTextField.tag)
if(textField.tag == 1) //dob text field is currently in edit mode
{
println("You are editing date of birth")
return true
}
else // name text field is in edit mode
{
return true
}
}
In XCode 6.3.2, I have a UITextField:
#IBOutlet weak var uiswitchControlledTextField: UITextField!
I am now using a UISwitch (named mySwitch) to control its enabled or disabled state in the following way:
#IBOutlet weak var mySwitch: UISwitch!
mySwitch.addTarget(self, action: Selector("stateChanged:"), forControlEvents: UIControlEvents.ValueChanged)
//callback below:
func stateChanged(switchState: UISwitch) {
uiswitchControlledTextField.enabled = switchState.on
}
The above works well, however, I am looking to try if it would be possible to create a UITextFieldDelegate to control the above UITextField in the same way. So far, I have the following by implementing textFieldShouldBeginEditing, in which I wish to return false to disable the UITextField, but I don't know how to let the UISwitch dynamically return true or false from textFieldShouldBeginEditing
import Foundation
import UIKit
class SwitchControlledTextFieldDelegate: NSObject, UITextFieldDelegate {
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
return false; //do not show keyboard or cursor
}
}
In ViewController, I try to set
self.uiswitchControlledTextField.delegate = SwitchControlledTextFieldDelegate()
but it does not work as I wished. Any help would be appreciated.
self.uiswitchControlledTextField.delegate = SwitchControlledTextFieldDelegate()
The problem is that that line merely creates an instance of your SwitchControlledTextFieldDelegate class, which then immediately goes right back out of existence.
You need to use, as your text field delegate, some instance which already exists and which will persist - like, perhaps, your view controller!
(Xcode 7)
Use this:
override func viewDidLoad() {
super.viewDidLoad()
// Setting the delegate
self.textField3.delegate = self
self.editingSwitch.setOn(false, animated: false)
}
// Text Field Delegate Methods
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
return self.editingSwitch.on
}
#IBAction func toggleTheTextEditor(sender: AnyObject) {
if !(sender as! UISwitch).on {
self.textField3.resignFirstResponder()
}
}
I have a textfield, when something is typed in the textfield and "return" on the keyboard is pressed, the keyboard should hide. But it doesn't..
Here is the code I am using:
import UIKit
class EditTableViewController: UITableViewController, UITextFieldDelegate {
var product: Product?
#IBOutlet weak var productImageView: UIImageView!
#IBOutlet weak var ProductDescriptionTextView: UITextView!
#IBOutlet weak var productTitleLabel: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
println("loaded")
productImageView.image = product?.image
productTitleLabel.text = product?.title
ProductDescriptionTextView.text = product?.description
}
override func viewWillDisappear(animated: Bool) {
product?.title = productTitleLabel.text
product?.description = ProductDescriptionTextView.text
product?.image = productImageView.image!
}
func textFieldShouldReturn(textField: UITextField) -> Bool // called when 'return' key pressed. return NO to ignore.
{
println("return")
return true
}
}
In the console I get "loaded", but when I press return in the textfield, I don't get "return"
how come?
You forgot to set the UITextField's delegate to your view controller (self)
productTitleLabel.delegate = self - also note that you should name your variables properly to avoid confusion (productTitleTextField instead of a 'Label' suffix)
Or, instead of doing it programmatically, you can do it in storyboard by Ctrl-dragging from your textView to the view controller in storyboard, and select delegate on the popup.
Then, let your view controller conform to UITextFieldDelegate protocol:
class EditTableViewController: UITableViewController, UITextFieldDelegate {
....
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == productTitleLabel {
textField.resignFirstResponder()
}
return true
}
}
In your viewDidLoad method you have to add:
productTitleLabel.delegate = self
And update your textFieldShouldReturn like this:
func textFieldShouldReturn(textField: UITextField) -> Bool // called when 'return' key pressed. return NO to ignore.
{
productTitleLabel.resignFirstResponder()
return true
}
And It will hide your keyboard when return key pressed.