0

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.

swift droid
  • 107
  • 1
  • 14
  • My gut feelings say that `UIControlEventEditingChanged` should only fire if the user edited the value manually (e.g. via tap/touch), but not due to some property setting in `viewDidAppear` or so. Please show more code of what exactly you are doing. – Andreas Oetjen Jan 17 '18 at 06:59

1 Answers1

0

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!

alujan
  • 150
  • 4