【问题标题】:How to link XIB buttons in a custom In-App Keyboard (Swift 4)如何在自定义应用内键盘(Swift 4)中链接 XIB 按钮
【发布时间】:2020-10-07 20:42:53
【问题描述】:

我刚刚创建了一个应用内自定义键盘,克服了所有困难,但现在被难住了(这可能是微不足道的):

如何连接 XIB 的按钮及其关联类,以便在 viewController 上的 UITextView 中输入适当的文本。

我创建了一个带有键盘的 XIB,以便我可以在不同的控制器中重复使用相同的键盘。设置所有约束、类、文件所有者等。一切正常 - 除了从键获取数据到视图。

这是我所拥有的:

class ViewController: UIViewController, UITextViewDelegate {

    @IBOutlet weak var inputField: UITextView!
    @IBOutlet weak var customKeyboard: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        inputField.delegate = self        
    }
}

class CustomKeyboard: UIView {

    @IBOutlet var contentView: UIView!
    
    @IBOutlet weak var key1: UIButton!
    @IBOutlet weak var key2: UIButton!
    // etc.
    @IBOutlet weak var keyReturn: UIButton!
    @IBOutlet weak var keyDelete: UIButton!
    
    
    override init(frame: CGRect) {
        // for using the custom view in code
        super.init(frame: frame)
        commonInit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        // for using the custom view in IB
        super.init(coder: aDecoder)
        commonInit()
    }
    
    
    private func commonInit() {
        Bundle.main.loadNibNamed("CustomKeyboard", owner: self, options: nil)
        contentView.frame = self.bounds
        contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        contentView.translatesAutoresizingMaskIntoConstraints = true
        addSubview(contentView)
    }
    
    
    @IBAction func key1(_ sender: UIButton) {
    }
    
    @IBAction func key2(_ sender: UIButton) {
    }

    // etc.

    @IBAction func keyReturn(_ sender: UIButton) {
    }
    
    @IBAction func keyDelete(_ sender: UIButton) {
    }
}

在我尝试的 CustomKeyboard 类中:

@IBAction func key1(_ sender: UIButton) {
    if let selectedRange: UITextRange = inputField.selectedTextRange {
        inputField.replace(selectedRange, withText: "1")
    }
}

@IBAction func keyDelete(_ sender: UIButton) {
    inputField.deleteBackward()
}

但是很明显,inputField 属于 ViewController 而不是 CustomKeyboard 类,所以我得到一个错误。

那么,如何将 CustomKeyboard XIB/class 上点击的字符输入到 ViewController 中的 textView 中?

【问题讨论】:

  • 这不是自定义键盘;这只是一种任性的看法。您需要一个带有inputView 的输入视图控制器,并且您将其设为文本视图的inputView。请参阅developer.apple.com/design/human-interface-guidelines/ios/… 并向下滚动到“自定义输入视图”。输入视图控制器自动连接到文本视图。见developer.apple.com/documentation/uikit/uiinputviewcontroller
  • 好的...有人建议我不要使用inputView。但是,如果可能的话,我喜欢使用它的想法,因为如果有人想要它,它还提供点击声音。但我不知道如何在我的情况下使用它..
  • 我不想要扩展名。我希望它成为这个应用程序中唯一可用的键盘,并且不需要在其他地方使用。
  • 是的,这就是我说向下滚动到“自定义输入视图”的原因。 - 好吧,我建议您首先弄清楚输入视图。该框架就是为此目的而存在的,那么为什么不利用它呢?用一个键做一个练习键盘。 :) 一旦你看到它是如何工作的,你就可以决定你是否喜欢这个架构。关键是,有一种方法可以替代虚拟键盘,而您正在做的不是它。
  • 这就是我在这里寻求帮助的原因——因为我自己也搞不清楚。你给我的链接非常有用,但没有告诉我如何实现。

标签: ios swift


【解决方案1】:

原来我只需要使用一个委托。将XIB中的所有按钮连接到keyTapped函数后:

class ViewController: UIViewController, UITextViewDelegate, KeyboardDelegate {

    @IBOutlet weak var inputField: UITextView!
    @IBOutlet weak var customKeyboard: CustomKeyboard!

    override func viewDidLoad() {
        super.viewDidLoad()

        inputField.delegate = self 
        inputField.inputView = UIView()

        customKeyboard.delegate = self     
    }

    func keyWasTapped(character: String) {        
    if character == "⌫" {
        inputField.deleteBackward()
    }
    else if character == "⏎" {
        inputField.insertText("\n")
    }
    else {
        inputField.insertText(character)
    }
    
}


protocol KeyboardDelegate: class {
    func keyWasTapped(character: String)
}

class CustomKeyboard: UIView {

    weak var delegate: KeyboardDelegate?

    @IBOutlet var contentView: UIView!
    
    @IBOutlet weak var key1: UIButton!
    @IBOutlet weak var key2: UIButton!
    // etc.
    @IBOutlet weak var keyReturn: UIButton!
    @IBOutlet weak var keyDelete: UIButton!
    
    
    override init(frame: CGRect) {
        // for using the custom view in code
        super.init(frame: frame)
        commonInit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        // for using the custom view in IB
        super.init(coder: aDecoder)
        commonInit()
    }
    
    
    private func commonInit() {
        Bundle.main.loadNibNamed("CustomKeyboard", owner: self, options: nil)
        contentView.frame = self.bounds
        contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        contentView.translatesAutoresizingMaskIntoConstraints = true
        addSubview(contentView)
    }
    
    @IBAction func keyTapped(sender: UIButton) {
    // When a button is tapped, send that information to the
    // delegate (ie, the view controller)
    self.delegate?.keyWasTapped(character: sender.titleLabel!.text!)
    }

    
    @IBAction func key1(_ sender: UIButton) {
    }
    
    @IBAction func key2(_ sender: UIButton) {
    }

    // etc.

    @IBAction func keyReturn(_ sender: UIButton) {
    }
    
    @IBAction func keyDelete(_ sender: UIButton) {
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    相关资源
    最近更新 更多