2015-11-03 34 views
7

我製作了應用內自定義鍵盤,取代了系統鍵盤,並在點擊UITextField時彈出。如何使用應用內自定義鍵盤的按鈕輸入文本

enter image description here

這裏是我的代碼:

class ViewController: UIViewController { 

    var myCustomKeyboard: UIView! 
    @IBOutlet weak var textField: UITextField! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let keyboardNib = UINib(nibName: "Keyboard", bundle: nil) 
     myCustomKeyboard = keyboardNib.instantiateWithOwner(self, options: nil)[0] as! UIView 

     textField.inputView = myCustomKeyboard 
    } 

} 

鍵盤佈局是從廈門國際銀行文件加載。

問題

如何獲取按鈕文本到文本字段?

注:

  • 有關於製造定製系統鍵盤(這需要安裝)很多教程,但我只想要一個在應用程序的鍵盤。這些教程僅爲鍵盤使用了特殊的視圖控制器,但在這裏,我似乎只是設置了鍵盤視圖。
  • 我已閱讀Custom Views for Data Input文檔。
  • This是我能找到的最接近的堆棧溢出問題,但它沒有描述如何從按鈕中獲取文本。

更新

  • This tutorial似乎表明,有自定義輸入視圖中的視圖控制器。但是,我迷失在Objective-C代碼中。 Swift中的過程是什麼?
  • This answer提到UITextField符合的UIKeyInput協議,但是如何使用它?
  • 如果還有任何內置的方式製作自定義應用內鍵盤,我真的更喜歡做一個正常的自定義視圖。
+0

您只需將按鈕標題文本字段:'的TextField.text =的TextField.text + button.title'? – ryancrunchi

+0

由於按鈕存儲在xib文件中,我如何獲得對它們的引用? – Suragch

+1

您可以爲鍵盤製作自定義視圖子類,並將這些按鈕設置爲公開,或者更好地實現委託。或者如果你想保留你的xib,在你的xib中給你的按鈕一個標籤。然後使用'myCustomKeyboard.viewWithTag(tag)' – ryancrunchi

回答

4

設置

  • 製作一個包含所有鍵的xib文件
  • 使用Autolayout,無論鍵盤稍後設置多大,鍵都會調整到正確的比例。
  • 創建一個與xib文件同名的swift文件,並將其設置爲xib文件設置中的文件所有者。

    enter image description here

    enter image description here enter image description here

  • 鉤出來鍵按鈕在swift文件的IBAction爲方法。 (參見下面的代碼。)

代碼

我使用的delegate pattern定製鍵盤視圖和主視圖控制器之間進行通信。這可以使它們分離。多個不同的自定義鍵盤可以交換進出,無需更改主視圖控制器中的詳細實現代碼。

Keyboard.swift文件

import UIKit 

protocol KeyboardDelegate { 
    func keyWasTapped(character: String) 
} 

class Keyboard: UIView { 

    var delegate: KeyboardDelegate? 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     initializeSubviews() 
    } 

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

    func initializeSubviews() { 
     let xibFileName = "Keyboard" // xib extention not needed 
     let view = NSBundle.mainBundle().loadNibNamed(xibFileName, owner: self, options: nil)[0] as! UIView 
     self.addSubview(view) 
     view.frame = self.bounds 
    } 

    @IBAction func keyTapped(sender: UIButton) { 
     self.delegate?.keyWasTapped(sender.titleLabel!.text!) 
    } 

} 

主視圖控制器

注意,ViewController符合我們創建的KeyboardDelegate協議。另外,在創建鍵盤視圖的實例時,需要設置height,但width不需要。顯然,設置文本字段的inputView會將鍵盤視圖寬度更新爲屏幕寬度,這很方便。

class ViewController: UIViewController, KeyboardDelegate { 

    @IBOutlet weak var textField: UITextField! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // get an instance of the Keyboard (only the height is important) 
     let keyboardView = Keyboard(frame: CGRect(x: 0, y: 0, width: 0, height: 300)) 

     // use the delegate to communicate 
     keyboardView.delegate = self 

     // replace the system keyboard with the custom keyboard 
     textField.inputView = keyboardView 
    } 

    // required method for keyboard delegate protocol 
    func keyWasTapped(character: String) { 
     textField.insertText(character) 
    } 

} 

來源

相關

+0

您完全正確使用此解決方案:)。只是一件小事:'設置如下的xib指標'不會影響運行應用程序時視圖的呈現方式。它只是在界面構建器中進行模擬,以瞭解如果在設置指標時如何呈現它們。 – ryancrunchi

+0

啊,不知何故,我錯過了「模擬」一詞的意義。感謝您指出了這一點。我從我的答案中刪除了這一步。 – Suragch

+0

實施完成。但是當我點擊按鈕時,它不會在'textfiled'中插入文本。它沒有來KeyWasTapped – Piraba

3

我想是這樣的:

一個新的功能來處理按鈕事件

func updateTextfield(sender: UIButton) { 
    textField.text = (textField.text ?? "") + (sender.titleForState(.Normal) ?? "") 
} 

和初始化您的自定義鍵盤後,註冊按鈕:

myCustomKeyboard.subviews 
    .filter { $0 as? UIButton != nil } // Keep the buttons only 
    .forEach { ($0 as! UIButton).addTarget(self, action: "updateTextfield", forControlEvents: .TouchUpInside)} 
相關問題