2015-11-03 43 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)} 
相关问题