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
Related
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
}
Environment: Xcode Version 8.2 (8C38)/Swift 3.0
A textFiled object in the View is wire up to a method named textFieldReturn in the controller via IBAction. The related codes are presented as follow
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func textFieldReturn(_ sender: Any) {
_ = (sender as AnyObject).resignFirstResponder()
}
}
What I expect:
When I hit the Return key of the virtual keyboard the function textFieldReturn(_:) will be called and the keyboard will be hidden
Issue Observer:
The function is not called after I tapped the return key, the keyboard is still there
Resource:
This code spinet come from the example of the Chapter 16 of the book iOS 10 App Development Essentials by Neil Symth (pp-114)
The only difference between this code and original code is the type of the function argument (Sender). It is AnyObject in the original book while I've got Any by default, therefore I've cast to AnyObject inside the function body
Question:
Its seems to be a decent book, but the sample code doesn't work for me. How can I call the resignFirstResponder() method when I hit the return key
Alternative try out:
Instead of using IBAction, I turn to the idea of delegate, I've set the VeiwController as the delegate of the textField
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var tempText: UITextField! //reference the TextField as the variable **tempText** in the controller
override func viewDidLoad() {
super.viewDidLoad()
self.tempText.delegate = self //set up the delegation
}
func textFieldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return(true)
}
}
Problem
The alternative solution still not working.
Thanks for your time and help
Why are you not using the original delegate function of UITextField?
I think the default function will work as you want:
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var tempText: UITextField! //reference the TextField as the variable **tempText** in the controller
override func viewDidLoad() {
super.viewDidLoad()
self.tempText.delegate = self //set up the delegation
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool{
textField.resignFirstResponder()
return false
}
}
Replace your textFieldReturn method with this and it should work just fine.
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
Make sure to keep the following in your viewDidLoad()
self.tempText.delegate = self
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:
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()
}
}