I have an UITextField which user sets its luggage weight as numbers in TextField. I want to set that textfields value with weight mark (for ex 10 KG which comes from user settings) so whatever user types, there will be KG mark at the end of its textfield. Is there any way for it?
do like
initially clear the value when begin start
func textFieldShouldBeginEditing(textField: UITextField) {
textField.text = ""
}
when editing is over append the kg
func textFieldDidEndEditing(textField: UITextField) {
yourTextfieldName.text = "\(textField.text!) KG"
}
Choice-2
func textFieldDidEndEditing(textField: UITextField)
{
if !textField.text!.rangeOfString("KG").location != NSNotFound {
self.textField.text = textField.text!.stringByAppendingString("KG")
}
}
You could place a "Label" next to the UITextField.
And then just change the text of the Label to whatever the user selects.
OR (but i dont know if that works), try to get the text form the textfield, add the unit (as a string) to the string from the textfield.
Related
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
I'm not sure if how I worded the question makes much sense so let me summarize.
I have 2 UITexfields and 2 UILabels.
I have it all working properly in terms of when I type into my first textField and hit return it will display my text.
Now I'm not sure how to get the same to apply to my other TextField and Label. I read that if I apply a tag to my textfield in Xcode "1" that I can apply tag "2" to my other textfield.
Here is the code I am using so when I press return it'll display textfield tag "1".
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField.tag == 1 {
nameDisplay?.text = textField.text!
}
return true
}
So to completely round this out, I want to display both separate textfields on each separate label.
Simply add an else to your if.
if textField.tag == 1 {
nameDisplay.text = textField.text
} else {
otherLabel.text = textField.text
}
Please note you do not need the ? or ! in that code.
Another option, if you have outlets for your text fields, is to do the following instead of bothering with tags:
if textField == someTextFieldOutlet {
nameDisplay.text = textField.text
} else {
otherLabel.text = textField.text
}
where someTextFieldOutlet is obviously needs to be the actual name of the appropriate text field outlet you have.
I have a UITextField that holds the text: "Username". I want to erase the user name once the user has selected the text field for editing.
Is there a better way than using the selector method?
Here is the method I'm currently using, but it doesn't seem to be working.
usernameTextField.addTarget(self, action: #selector(selectedUsernameField), for: .editingChanged)
func selectedUsernameField(sender: UITextField){
print("selectedUsernameField")
usernameTextField.text = ""
}
I this case you should set the placeholder for usernameTextField.
usernameTextField.placeholder = "UserName"
It will disappear as user will start typing that's what you want.
You can use this function:
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == usernameTextField{
textField.text = ""
}
}
It is called when the text field begins editing.
Although to use it you will need to make your class a UITextFieldDelegate
and also say:
usernameTextField.delegate = self
in viewDidLoad.
Requirement
I want to block a text field (UITextField) while another text is being entered in another text field.
Example
I have two text fields on a form. When typing in the first text field, the second text field is disabled (i.e. you can not even press it by touching), only when you finish editing the first text field, you can edit the second one.
You can add your textfield to an an array and use the Equatable protocol to compare the textfield in the DidBeginEditing func and "disable" other fields. But when you are done editing you need to turn all your textfields back on. Also make sure your class has UITextFieldDelegate in the declaration.
var textfields: [UITextField] = [textfield1,textfield2,textfieldn]
func textFieldDidBeginEditing(textField: UITextField) {
for field in textfields {
if textField != field {
field.enabled = false
}
}
}
func textFieldDidEndEditing(textField: UITextField) {
for field in textfields {
field.enabled = true
}
}
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
}