Swift - Trouble with TextField.clearsOnBeginEditing - swift

override func viewDidLoad() {
super.viewDidLoad()
self.enterNumberTextField.clearsOnBeginEditing = true
}
Setting this bool true does not persist in later code, and i do not reset it in any of my code. Setting the bool does not force the text box to clear when the user puts the cursor in it. I check the value of the bool and it is true on the first pass through code, though the text box does not clear. On subsequent passes through code the bool is reset to false. not by my code.
I have also tried putting this line of code into the IBAction function where all the processing is done in this program. Same result. Does not clear text box and is reset to false inside the if {} where all the work is done.
Can someone tell me why this is happening, and if there is a place where this bool should be set to true such that it persists throughout the program's execution.

From the Swift documentation:
Even if this property is set to true, the text field delegate can override this behavior by returning false from its textFieldShouldClear: method.
Does your view adhere to the UITextFieldDelegate protocol? Do you implement the textFieldShouldClear method? This sounds like it could be the source of your problem.
EDIT:
What happens if you do something like this:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
textField.clearsOnBeginEditing = true
textField.delegate = self
}
func textFieldShouldClear(textField: UITextField) -> Bool {
return true
}
}
That is, add the UITextFieldDelegate protocol at the top, and then implement textFieldShouldClear so it always returns true. This may not be what you ultimately want, but it might solve the issue. Check out the Swift documentation for more info on protocols and delegation.

The problem was I only had one text field and was never giving up focus. I added another text field to transfer focus to , now when I put focus back in the text field of interest, it does clear. Simple, must transfer focus for that method to work.

Related

How do you get a text field input in you view controller code?

I’m trying to make Xcode print "Nice!" when you type in "Hi". I've used a IBOutlet, but I don’t know how to use the user input in my code. Also BTW I'm using Storyboard and not SwiftUI. It also gives me an error when I try to compare the datatype UIViewController and a String. Here is my view controller code(with the default App Delegate and Scene Delegate code):
import UIKit
class ViewController: UIViewController {
#IBOutlet var yeet: [UITextField]!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func fuel(_ yeet:UIViewController) -> Int {
if yeet == ("hi") {
print("Nice!")
}
}
}
your textfield show be setup as
#IBOutlet weak var textFeildName: UITextField!
you will need to change a couple things inside of your file to prevent a crash. I'd delete the textfield and drag it into the assistant view and give it a new name.
but before you press "connect" press the "outlet" tab and change it to "Action" and then a new selector should come up select "Editing Did End" and go to the top and press "Did End On Exit"
after that is done would want to reference the variable of the text field:
example:
#IBAction func TextFieldName(_ sender: Any) {
if(self.TextFeildName.text!.contains("hi")){
print("Nice!")
}
}
On top of all this, you do not compare strings with == that's only if you compare 2 separate strings for example stringOne == stringTwo if you are comparing or asking if a string contains anything you'd want to use the developing language specific string container IE: .contains
Also, please do not include "Xcode" as a tag with your question, as that should be reserved for Xcode related problems. not Swift or objective-c coding issues.

How to create a custom UITextField prepackaged with its own formatting behavior?

I am trying to make a reusable textfield that will format its contents to be in a currency format without relying on a containing viewcontroller to implement this behavior for it.
Right now the viewcontroller that the textfield is in is implementing the desired behavior in a TextFieldChange action and it works fine:
class MyViewController: UIViewController {
#IBOutlet weak var TextField: UITextField!
...
...
#IBAction func TextFieldChanged(_ sender: Any) {
// Format text in text field
}
}
This works, but that means I'll have to copy and paste this code into every viewcontroller that I want to have this functionality. I would like it so I could just assign the textfield its own class in the inspector so that this behavior will be a part of every textfield I make using this class. How do I do this?

Detect backspace in empty UITextField Swift Working Sample

I know that this question may sounds duplicate, but looking at the several answer, nothing is working for me yet.
Basically I have a Quizz App, a question is shown and the user needs to fill several UITextFields to answer the question (i.e. if the answer is VENICE, 6 UITextFields will be shown, 1 per letter).
It was able to detect one character in the UITextFields and once the user hits a key it will jump to the following UITextField. I use the tag of the UITextField and the method becomeFirstResponder.
The problem is that I will like to detect the backspace when a UITextField is empty so I will jump to the previous UITextField.
I have tried this solution from Jacob Caraballo (Detect backspace in empty UITextField) but I am not sure, how to use it with my existing UITextField.
For example, I have tried:
// #IBOutlet weak var textField1: UITextField!
#IBOutlet weak var textField1:MyTextField!
But calling the delegate
textField1.myDelegate = self
It crashes
I also notice, that using the Jacob's solution I won't be able to check which UITextField was used as the func textFieldDidDelete() doesn't not have an UITextField as parameter and I will need to check its tag i.e.
If textField.tag == 5 {
textField4.becomeFirstResponder()
}
Any help on this please?
If you use Jacob's solution, the deleteBackward method will be called with the current UITextField instance. So you can add a protocol to that UITextField class and pass it back to your view.
Something like this:
protocol MyTextFieldDelegate: class {
func backwardDetected(textField: MyTextField)
}
class MyTextField: UITextField {
weak var myTextFieldDelegate: MyTextFieldDelegate?
override func deleteBackward() {
super.deleteBackward()
self.myTextFieldDelegate?.backwardDetected(textField: self)
}
}

Disable keyboard in UIwebView textField in swift

I wrote a webApp in html/css/js using another text editor, then copied the text over to the relevant files in an existing Xcode project.
The project runs inside a webView alright "left image", but when I click inside a text field "right image" part of the stock keyboard covers the 2 bottom buttons.
I expected to see a fully fledged keyboard, but I really would like to disable it all together, because I will provide my own later.
How to disable the stock keyboard when a webView textField is tapped in this case?
Swift 2.1 and Xcode 7.2.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var mainWV: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
//load a file
let localfilePath = NSBundle.mainBundle().URLForResource("index", withExtension: "html");
let myRequest = NSURLRequest(URL: localfilePath!);
mainWV.loadRequest(myRequest);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Try make use of UIView superclass (superclass of UIWebView as well ass super-super of UITextField) property .userInteractionEnabled:
myWebViewTextField.userInteractionEnabled = false
Alternatively, if this is not what you're looking for, try the property .enabled from superclass UIControl of UITextField:
myWebViewTextField.enabled = false
UIView.userInteractionEnabled Property
A Boolean value that determines whether user events are ignored and
removed from the event queue.
From the Language Reference for UIVIew.
UIControl.enabled Property
A Boolean value that determines whether the receiver is enabled.
From the Language Reference for UIControl.

Move a label left from its position in swift

I have put a label in a text box and I want to shift this label to left side after tapping this text box. In the following screenshot 'hello' is a label and I want to shift it left after tapping on the text box.
I wrote the following code but the 'textFieldDidBeginEditing' function is not called
#IBOutlet weak var lblhello: UILabel!
func textFieldDidBeginEditing(numtxt: UITextField!) { //delegate method
lblhello.frame = CGRectMake(98, 156, 42, 21)
}
I'm making the assumption based on previous comments that you want to manually specify the frame size, and aren't using constraints.
First off, you need to add UITextFieldDelegate to your viewController.
This is done like so in the beginning of your VC class:
class yourViewControllerName: UIViewController, UITextFieldDelegate {
Then you want to specify the delegate of your UITextField, (most likely in viewDidLoad)
yourTextFieldName.delegate = self
Now you can use the textFieldDelegate functions. The on you're looking for is probably this:
func textFieldDidBeginEditing(textField: UITextField) -> Bool {
lblhello.frame = CGRectMake(160, 200, 60, 40)
return true
}
If everything is set up correctly that should change the frame of lblhello when a user begins editing your UITextField.
One common problem I often hear for people saying this doesn't work. Is caused by the label being specified in viewDidLoad(). Most of the time you want to declare the label before viewDidLoad(), otherwise functions like this doesn't have access to it.
In case you want to move it back to it's original position afterwards. You do almost the same as you did in textFieldDidBeginEditing except you use textFieldDidEndEditing instead.
However, on a side-note. I do suggest getting used to using constraints rather than setting the frame for things like this.