单击后退按钮完成UITextField的编辑 [英] Finish editing UITextField on back button tap

查看:98
本文介绍了单击后退按钮完成UITextField的编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在NavigationController中有2个控制器。第一个将第二个推入堆栈,用户可以在此处与文本字段进行交互。然后(在一种情况下)用户将单击后退按钮以转到上一个屏幕。假设第二个负载很大,那么一旦需要,我将只保留其中一个实例。

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 tap on back button to be taken to the previous screen. Assuming that loading of second one is 'heavy', so I will be keeping only one instance of it once it is needed.

预期:
按下后退按钮后,我想隐藏键盘。

Expected: I would like to have keyboard hidden once back button is pressed.

实际:
当我第二次回到第二个时,第一响应者一直处于恢复状态。如何预防呢?辞去急救人员的工作也无法解决问题……

Actual: First responder keeps being restored when I go back to the second for the second time. How to prevent that? Resigning first responder also doesn't do the trick there...

问题演示:
https://gitlab.com/matrejek/TestApp

主要代码部分:

class FirstViewController: UIViewController {
    var child: UIViewController = {
        let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        let vc = storyboard.instantiateViewController(withIdentifier: "child")
        return vc
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func onButtonTap(_ sender: Any) {
        self.navigationController?.pushViewController(child, animated: true)
    }
}

class SecondViewController: UIViewController {
    @IBOutlet weak var textField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        view.endEditing(true)
    }
}


推荐答案

这确实很奇怪---看来您的方法应该有效。

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

显然(基于快速测试),由于不允许导航控制器释放SecondVC,因此文本字段保持活动状态。

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

如果将其添加到 SecondViewController 中,将防止键盘在您下次导航到控制器时自动重新显示-不确定它是否适合您,但是会完成这项工作:

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)
    }
}






编辑: 2020年1月25日

根据新评论,是的,这个似乎是一个错误。

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

我先前的变通方法工作正常-有点。结果是键盘弹出,然后在随后按下 child 时消失。

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

以下是更好的方法-周围。我们使 SecondViewController 符合 UITextFieldDelegate 并添加一个BOOL类var /属性,该属性将防止文本字段成为第一响应者。注释应该清楚:

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
    }
}

这篇关于单击后退按钮完成UITextField的编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆