【问题标题】:Immediately change first responder when activating UITextField激活 UITextField 时立即更改第一响应者
【发布时间】:2017-05-05 03:54:44
【问题描述】:

我在这个问题上纠结了一段时间。

我有一个inputAccessoryView 我想用作我的键盘,而inputAccessoryView 里面有一个UITextField。这是应该激活键盘的文本字段,但不能,因为它存在于 inputAccessoryView 中。

我通过视图控制器的viewDidLoad 添加了第二个UITextField 到托管视图。第二个UITextField 被隐藏起来,只是通过becomeFirstResponder 激活键盘的一种方式。

我为我的自定义inputAccessoryView 设置了一个委托,当用户关闭属于附件视图中存在的文本字段的键盘时,该委托会被调用。我在这个委托函数中有代码到第二个隐藏键盘上的resignFirstResponder。这很好用。

我目前遇到的问题是在becomingFirstResponder 时出现类似的行为。我想这样做,所以当隐藏的键盘成为第一响应者时,它会立即在附件视图中的第二个键盘上调用第一响应者,让它看起来像是刚被激活。

我不确定如何实现这一点。我尝试在我的视图控制器上实现 UITextField's 委托并覆盖 textFieldDidBeginEditing 并在那里进行 becomeFirstResponder 调用,但无济于事。

任何解释为什么这不起作用,或采取其他方法?

编辑:我遵循了here 的建议。它确实允许第一响应者所有权转移,但这样做有点生涩(以前的键盘仍然显示一瞬间),现在以前运行良好的resignFirstResponder 进程根本不起作用。我猜textFieldDidBeginEditingresignFirstResponder 通话期间被触发并再次调用becomeFirstResponder。我现在无法关闭键盘。

【问题讨论】:

    标签: ios objective-c swift uitextfield


    【解决方案1】:

    在开始一个新的空项目并四处寻找之后,我想出了以下解决方案,到目前为止,它似乎运行良好且符合预期。

    import UIKit
    
    class ViewController: UIViewController, UITextFieldDelegate {
    
        @IBOutlet weak var textField: UITextField!
        var accessory: CustomView!
        var denyFirstResponder = false
        var dismissTap: UITapGestureRecognizer!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            accessory = CustomView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 44))
            textField.inputAccessoryView = accessory
            textField.delegate = self
            dismissTap = UITapGestureRecognizer(target: self, action: #selector(shouldDismiss))
            view.addGestureRecognizer(dismissTap)
        }
    
        func shouldDismiss(_ recognizer: UITapGestureRecognizer) {
            accessory.textField.resignFirstResponder()
        }
    
        @IBAction func buttonPressed(_ sender: UIButton) {
            textField.becomeFirstResponder()
        }
    
        func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
            if denyFirstResponder {
                denyFirstResponder = false
                return false
            }
            return true
        }
    
        func textFieldDidBeginEditing(_ textField: UITextField) {
            denyFirstResponder = true
            accessory.textField.perform(#selector(becomeFirstResponder), with: nil, afterDelay: 0)
        }
    
    }
    
    class CustomView: UIView {
    
        var textField: UITextField!
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            backgroundColor = UIColor.blue
            textField = UITextField(frame: CGRect(x: 10, y: 10, width: frame.width - 20, height: 24))
            addSubview(textField)
            textField.keyboardType = .decimalPad
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-03
      • 2018-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多