0

I have a tableView cell with textfield, which also show picker, where I displaying filtered list of data, and it filter by input symbols. I'm using IQkeyboardManager and McPicker. When user begins editing, picker is presenting as modal. Then, if value chosen from picker's list, picker is dismissed.

And problem is: when I tap done at keyboard (calling didEndEditing) at console I see as app calling didEndEditing - then beginEditing, and then processing lines of code at @IBAction func editDidBegin(_ sender: UITextField) again

here is my code:

class QuickOrderVC: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {
/* some code */
@IBAction func editDidBegin(_ sender: UITextField) {
        print("didBegin")
        /* some code */
        McPicker.showAsPopover(data: [filteredArr], fromViewController: self, sourceView: sender as! UIView, doneHandler: { [weak self] (selections: [Int : String]) -> Void in
            if let first = selections[0]{
                /* some code */
                self?.dismissKeyboard()
                return
            }
            }, cancelHandler: { () -> Void in
                /* some code */
                print("Canceled Popover begin")
                self.dismissKeyboard()
        }, selectionChangedHandler: { (selections: [Int:String], componentThatChanged: Int) -> Void  in
            let newSelection = selections[componentThatChanged] ?? "Failed to get new selection!"
            print("Component \(componentThatChanged) changed value to \(newSelection)")
        })
    }
    var filteredArr = [String]()
    @IBAction func sizeTextEditingChanged(_ sender: UITextField) {
        /* some code */
        self.presentedViewController?.dismiss(animated: false, completion: {
            McPicker.showAsPopover(data: [filteredArr], fromViewController: self, sourceView: sender as! UIView, doneHandler: { [weak self] (selections: [Int : String]) -> Void in
                if let first = selections[0]{
                    /* some code */
                    print("done Popover edit")
                    self?.presentedViewController?.dismiss(animated: false, completion: nil)
                    print("done Popover dismiss")
                    /* some code */
                    self?.dismissKeyboard()
                    return
                }
                }, cancelHandler: { () -> Void in
                    /* some code */
                    print("Canceled Popover edit")
                    self.presentedViewController?.dismiss(animated: false, completion: nil)
                    print("Canceled Popover dismiss")
                    self.dismissKeyboard()
                    return
            }, selectionChangedHandler: { (selections: [Int:String], componentThatChanged: Int) -> Void  in
                let newSelection = selections[componentThatChanged] ?? "Failed to get new selection!"
                print("Component \(componentThatChanged) changed value to \(newSelection)")
            })
        })
    }
    @IBAction func editDidEnd(_ sender: UITextField) {
        print("did end")
        self.presentedViewController?.dismiss(animated: false, completion: nil)
        print("did end - dismiss")
        return
    }

and at console:

did end // when tap `done` at keyboard
didBegin // why??
did end - dismiss
did end //why duplicate??
did end - dismiss
done Popover edit
done Popover dismiss
nastassia
  • 807
  • 1
  • 12
  • 31
  • Are you really needed the functionality to edit the value of textField with Keyboard? – Febin Fathah May 20 '19 at 12:22
  • @FebinFathah yes, i update filtered list of data as user input something. this part works fine – nastassia May 20 '19 at 12:24
  • 1
    When printing the debug lines, you should be adding the `sender` value to the output string. If not now, in the future, you will have multiple calls from different UITextFields that you will need to distinguish. – AgRizzo May 20 '19 at 12:28
  • @AamirR can you make a suggestion how to change this lines at mcpicker.swift (which actually a cocoapod library)? I also use this picker at another viewcontrollers without this error – nastassia May 20 '19 at 13:07
  • @AamirR oh, I mean what changes should I do to prevent the error? like add `guard `statement or smth? – nastassia May 20 '19 at 14:06
  • 1
    Add an exception breakpoint and identify the variable getting nil while unwrapping. – Febin Fathah May 21 '19 at 05:48

0 Answers0