簡體   English   中英

(Xcode 8 Swift 3)。 對特定文本字段使用自定義鍵盤擴展

[英](Xcode 8 Swift 3). Using Custom Keyboard Extension with a particular Text Field

在此處輸入圖片說明

由於標准數字鍵盤有“空”按鈕,但沒有“+/-”按鈕,我決定創建自己的鍵盤擴展。 我已經做到了。 在此處輸入圖片說明

但我不知道如何將它與特定的文本字段鏈接(和調用),而其他文本字段使用常用鍵盤。 有沒有機會像我自己的自定義鍵盤一樣應用自定義鍵盤類型?

我找到了基於類似問題的解決方案: 如何使用應用內自定義鍵盤的按鈕輸入文本

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
    }

}

/* 當錯誤:“Could not load NIB in bundle” Could not load NIB in bundle在文件檢查器中訪問 .xib 文件的屬性,選擇框上的“Target Membership”屬性,然后你的 xib 文件被鏈接與你的目標 */

在主 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
        }
        */
    }

}

在此處輸入圖片說明

我為公制/英制系統編寫了一個計算器。 為此,我也編寫了自己的鍵盤。

在此處輸入圖片說明

我已將鍵盤設置為堆疊在堆棧視圖中的UIButtons

在此處輸入圖片說明

由於我剛剛升級到 Xcode 8 並且該項目仍在 Swift 2.2 中,因此 Xcode 似乎不知道這些按鈕。

在此處輸入圖片說明

然后我設置了一個 UITextField 並使用我的按鈕填充了它的text屬性。 例如,這是我鍵盤上按鈕1的功能。

@IBOutlet weak var inputField: UITextField!
var numberStr:String = "0"
inputField.text = numberStr

@IBAction func oneKeyboardAction(sender: AnyObject) {
    if numberStr == "0" {
        numberStr = String(numberStr.characters.dropLast())
    }
    let newStr:String = numberStr + String("1")
    numberStr = newStr
    let dotToCommaString = newStr.stringByReplacingOccurrencesOfString(".", withString: ",")
    inputField.text = dotToCommaString
}

此外,我已停用與 TextField 的用戶交互,因此不會顯示“原始鍵盤”。

編輯

就像評論部分中提到的那樣,讓我的答案更適合您的需求。 您可以將我的自定義鍵盤設置為與界面生成器內的UIViewController重疊的UIView viewDidLoad() MyKeyboardView.hidden = true其設置為MyKeyboardView.hidden = true

然后你有你的 TextField 你希望自定義鍵盤可見而不是系統鍵盤:

func textFieldDidBeginEditing(_ textField: UITextField) {
    if (textField == self.yourDesiredTextField) { //the one where you want to use the custom keyboard 
        MyKeyboardView.hidden = false
    }
}

然后你添加一個這樣的手勢識別器:

override func viewDidLoad() {
        super.viewDidLoad()

        MyKeyboardView.hidden = true


        let tap = UITapGestureRecognizer(target: self, action: #selector(gesture))
        tap.numberOfTapsRequired = 1
        view.addGestureRecognizer(tap)
}

func gesture() {

        UIView.animateWithDuration(0.2) {
            self.MyKeyboardView.hidden = true
        }

}

為了讓它更流暢,我在隱藏鍵盤時添加了animateWithDuration

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM