Finish editing UITextField on back button tap

前端 未结 1 482
刺人心
刺人心 2021-01-14 08:33

I have 2 controllers inside NavigationController. First pushes the second one to the stack and user can interact with the text field there. Then (in one scenario) user will

相关标签:
1条回答
  • 2021-01-14 08:57

    This does seem odd --- and it seems like your approach should work.

    Apparently (based on quick testing), since you are not allowing the Navigation Controller to release the SecondVC, the text field is remaining "active."

    If you add this to SecondViewController, it will prevent the keyboard from "auto re-showing" the next time you navigate to the controller - not sure it will be suitable for you, but it will do the job:

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        DispatchQueue.main.async {
            self.view.endEditing(true)
        }
    }
    

    Edit: Jan 25 2020

    Based on new comments, yes, this seems to be a bug.

    My previous work-around answer worked -- sort of. The result was the keyboard popping up and then disappearing on subsequent pushes of child.

    Following is a better work-around. We have SecondViewController conform to UITextFieldDelegate and add a BOOL class var / property that will prevent the text field from becoming first responder. Comments should be clear:

    class FirstViewController: UIViewController {
        var child: UIViewController = {
            let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
            let vc = storyboard.instantiateViewController(withIdentifier: "child")
            return vc
        }()
    
        @IBAction func onButtonTap(_ sender: Any) {
            self.navigationController?.pushViewController(child, animated: true)
        }
    }
    
    // conform to UITextFieldDelegate
    class SecondViewController: UIViewController, UITextFieldDelegate {
        @IBOutlet weak var textField: UITextField!
    
        // bool var to prevent text field re-becoming first responder
        // when VC is pushed a second time
        var bResist: Bool = false
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // assign text field delegate
            textField.delegate = self
        }
    
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            // view has appeared, so allow text field to become first responder
            bResist = false
        }
    
        func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
            return !bResist
        }
    
        override func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)
    
            // end editing on this view
            view.endEditing(true)
    
            // we want to resist becoming first responder on next push
            bResist = true
        }
    }
    
    0 讨论(0)
提交回复
热议问题