Initially button.isHidden = true, however, I would like to set the button.isHidden = false, when all textfields are filled. Where should I put the If-Else condition in, most probably not in viewDidLoad, please advise.
You might want to take a look at this post UITextField text change event
and then in textFieldDidChange function you should check texts of each textfield
Related
I was wondering how I would be able to go about changing the status of buttons. I want to make it if one of two text fields has text in them then the button will become useable. I have currently turned off the button from the storyboard. The code I have to check if there is text inside of the text fields is as follows:
Disclaimer:
The code to check if the text field has any text in it works perfectly fine.
#IBAction func textFeildEditingChanged(_ textField: UITextField) {
if FirstName.hasText == true {
self.navigationItem.rightBarButtonItem?.isEnabled = true
print("First name isn't empty")
}
}
The current code that I have in there to set the button to enabled and disable doesn't work however the code to test if the text field has content does work. I just need to figure out how to disable and enable the button of a navigation item.
code that doesn't work is below:
self.navigationItem.rightBarButtonItem?.isEnabled = true
Any help would be greatly appreciated.
Edit: I am using a navigation controller, don't know if that's important or not.
If you are using the storyboard, just connect the outlet then disable it.
#IBOutlet var navigationItemButton: UIBarButtonItem!
then
navigationItemButton.isEnabled = FirstName.hasText
I have a UIViewController with several UITextFields. When tap one text field, it should present the barcode scanning view controller. Once the scanning is completed, my barcode scanning viewcontroller is disappearing (used "dismissViewcontroller") and the scanned value should entered into the text field I tapped. This is working fine. I have set the delegate for each text field like this.
[field addTarget:metrixUIViewControllerIn action:#selector(executeScriptOnTextFieldChange:) forControlEvents:UIControlEventEditingChanged];
The problem is this :
Lets say I have set an alert to display inside this executeScriptOnTextFieldChange method. Once I tapped on the 1st text field, then the barcode scanner comes. Once I scanned barcode scanner closes and set the value for the first text field and fire the alert.Thats ok. But then if scanned by tapping the 2nd textfield and the string will set to that textfield and fire the alert related to 2nd textfield also fire the alert related to first textfield as well. I want to stop happening this. Is there any way to disable the delegate for one textfield? This happens because I am refreshing the view in the viewDidAppear. But I have to do that as well. Please help me.
UIControlEventEditingChanged for a textField can fire at many different events that are not even directly related to that textField, but related inderectly.
For instance, when your ViewController is presenting the barcodeScanner it may trigger a "resignFirstResponder" event on the textField. Also when the 2nd textField is tapped, cause the 2nd becomes first responder and the 1st suffers a "resignFirstResponder".
I suggest trying to use a UITapGestureRecognizer in your textField instead. Example:
Swift 4
#IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.textField.tag = 1
self.textField.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(fireTextField(_:))))
}
#objc func fireTextField(_ sender: UIGestureRecognizer){
let view = sender.view
guard view != nil else{
//Do nothing
return
}
let condition = view!.tag == 1
if condition{
//or do whatever other stuff you need
self.textField.becomeFirstResponder()
}else{
//Whatever for other textFields
}
}
This way, you could use the "tag" attribute to determine which textField is firing and so adjust "condition". You could also filter the flow with a switch using the "tag".
Not sure if any of this will really help as I would need more info about the flow you need to accomplish. Hope it does help!
do you know how I can make an edittext get non-editable for the user, but allow it to use the textfield's own clearbutton to delete it?
I tried using the:
textField.isEnabled = false
and
textField.isUserInteractionEnabled = false
but it did not work very well because it disables everything
Return false from the text field delegate’s textFieldShouldBeginEditing.
I have a textfield on a form where the value changes based on the slider. I am trying update the functionality so that when the user enters a value in the text field the slider will move to the correct position. My UISlider will be a value between 0 and 3.
I have been trying to use this post to help me but, I have not been able to get it to work: How to set value of UISlider from a UITextField
Cheers,
I want the user to be able to use the slider or keyboard to enter the rate discount - but if they enter a value I want the slider to move to the right spot. Using the code given the app crashes. I think it's because of conflicting code. I have been linking the sider as an outlet. Is this correct?
Are you failing because yoru link is an Objective C version? So here is the swift equivalent. First, add a listener on your textfield like this
textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
Then in your text field text change function, set value for your slider
func textFieldDidChange(textField: UITextField) {
if let stringValue = textField.text{
if let intValue = Int(stringValue){
slider.setValue(intValue, animated: true)
}
}
}
In one function in my Swift code, I need to wait until user presses a button. Otherwise, I just do nothing. How do I do this? Maybe button press triggers some event I can catch.
P.S. Due to structure of my code, doing what I want on this button press (like #IBAction) is not an option!
Declare a global boolean value such as var buttonPressed = false and then, in the IBAction of the button (When you click it):
if buttonPressed = false{
buttonPressed = true
}
And in the method you want to call when the button is pressed, just ask if buttonPressed is true.
Try that and tell me if it worked
You can set the user interaction to false by default of your main UIView (usually the UIViewController view)
self.view.userInteractionEnabled = false
and when you have the action of button you set the user interaction to true.
self.view.userInteractionEnabled = true
I might be too late but personally, there are two way you can achieve this.
Add Observer
Use delegate pattern so whenever user tap a button you notify your delegate