(Xcode 8 Swift 3).对特定的文本字段使用自定义键盘扩展 [英] (Xcode 8 Swift 3). Using Custom Keyboard Extension with a particular Text Field

查看:82
本文介绍了(Xcode 8 Swift 3).对特定的文本字段使用自定义键盘扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于标准数字键盘的键盘具有空"按钮,而没有"+/-"按钮,因此我决定创建自己的键盘扩展名.我做完了

Since standard Number Pad keyboard has "empty" button, but doesn't have "+/-" button, I decided to create my own Keyboard Extension. I've done it.

但是我不知道如何使用特定的键盘将其链接(和调用)到特定的文本字段,而其他文本字段则如何使用普通键盘. 有没有机会像我自己的自定义键盘一样应用自定义keyboardType?

But I don't know how to link (and invoke) it with a particular Text Field while other Text Fields using usual keyboards. Is there any opportunity to apply custom keyboardType like my own custom Keyboard?

推荐答案

我发现了基于类似问题的解决方案:

I found solution based on simular question: How to input text using the buttons of an in-app custom keyboard

import UIKit

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

class KeyboardView: UIView {

    // This variable will be set as the view controller so that
    // the keyboard can send messages to the view controller.
    weak var delegate: KeyboardDelegate?


    // MARK:- keyboard initialization
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        initializeSubviews()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        initializeSubviews()
    }

    func initializeSubviews() {
        let xibFileName = "KeyboardView" // xib extention not included
        let view = Bundle.main.loadNibNamed(xibFileName, owner: self, options: nil)?[0] as! UIView
        self.addSubview(view)
        view.frame = self.bounds
    }

    // MARK:- Button actions from .xib file    
    @IBAction func keyTapped(_ sender: UIButton) {
        // When a button is tapped, send that information to the
        // delegate (ie, the view controller)
        self.delegate?.keyWasTapped(text: sender.titleLabel!.text!) // could alternatively send a tag value
    }

}

/*,当错误:无法在捆绑包中加载NIB" 无法在捆绑包中加载NIB 在文件检查器中访问.xib文件的属性,在选择框上单击目标成员资格"属性,然后将xib文件与目标链接 */

/* when error: "Could not load NIB in bundle" Could not load NIB in bundle Visit the properties of the .xib files in the file inspector ,the property "Target Membership" pitch on the select box ,then your xib file was linked with your target */

在主ViewController.swift中

In main ViewController.swift

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, KeyboardDelegate {

    @IBOutlet weak var text1: UITextField!
    @IBOutlet weak var text2: UITextField!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        // initialize custom keyboard
        let keyboardView = KeyboardView(frame: CGRect(x: 0, y: 0, width: 375, height: 165))
        keyboardView.delegate = self // the view controller will be notified by the keyboard whenever a key is tapped

        // replace system keyboard with custom keyboard
        text1.inputView = keyboardView //accessoryView
        text1.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // required method for keyboard delegate protocol
    func keyWasTapped(text character: String) {

        if Int(character) != nil{
            text1.insertText(character)
        }

        if character == "⌫" {
            if !(text1.text?.isEmpty)! {
                let beforeText = text1.text!
                let truncated = beforeText.substring(to: beforeText.index(before: beforeText.endIndex))
                text1.text = truncated

            }
        }

        if character == "±" {
            let beforeText = text1.text!
            if var number = Int(beforeText) {
                number = -number
                text1.text = "\(number)"
            }
        }


    }

    func textFieldDidBeginEditing(_ textField: UITextField) {
        /*
        if (textField == self.text1) {
            //textField.inputView = keyboardView
        }
        */
    }

}

这篇关于(Xcode 8 Swift 3).对特定的文本字段使用自定义键盘扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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