问题描述
我花了几个小时试图找出如何创建/ 获取自定义 inputView 工作。
我有一个 TextInputs (认为拼字板)的网格,当按下时应加载自定义 inputView 插入文本。
我创建了一个 .xib 文件,其中包含 UI元素 inputView 。我能够创建一个 CustomInputViewController ,并且显示 inputView ,但从来不能得到实际的 TextInput 更新为 value / text 。
Apple文档似乎已经阐明了如何让它工作,我看到的许多教程已经使用Obj-C(我已经无法转换由于小的事情,似乎无法现在在swift中完成)。
什么是总体架构和必要的代码片段应该实现创建一个<$多个 textInputs (委托链,控制器,.xibs,视图等)的 customInputView ?
使用适当的inputView布局和项目设置一个nib文件。在我的情况下,我将每个按钮设置为 inputViewButtonPressed 的文件所有者上的操作。
设置故事板
然后使用下面的代码,你应该得到你正在寻找的东西:
class ViewController:UIViewController,UITextFieldDelegate {
var myInputView:UIView!
var activeTextField:UITextField?
override func viewDidLoad(){
super.viewDidLoad()
//从nib加载输入视图
如果let objects = NSBundle.mainBundle ).loadNibNamed(InputView,owner:self,options:nil){
myInputView = objects [0] as UIView
}
//设置所有文本字段与我们作为委托和
//使用我们的输入视图
for view in self.view.subviews {
if let textField = view as? UITextField {
textField.inputView = myInputView
textField.delegate = self
}
}
}
func textFieldDidBeginEditing(textField:UITextField) {
activeTextField = textField
}
func textFieldDidEndEditing(textField:UITextField){
activeTextField = nil
}
@ IBAction func inputViewButtonPressed(button:UIButton){
//使用按钮的文本更新文本字段
activeTextField?.text = button.titleLabel?.text
/ /关闭文本字段
activeTextField?.resignFirstResponder()
}
}
$ b b
或者,如果你想要更像键盘一样,你可以使用这个函数作为你的动作(它使用Swift 1.2的新的let语法),如果你需要1.1兼容性,分解它:
@IBAction func insertButtonText(button:UIButton){
if let textField = activeTextField,title = button.titleLabel? range = textField.selectedTextRange {
//使用按钮按下的文本更新文本字段
textField.replaceRange(range,withText:title)
}
}
这使用 UITextInput 协议将文本字段更新为适当。处理删除有点复杂,但仍然不太糟糕:
@IBAction func deleteText(button:UIButton){
if let textField = activeTextField,range = textField.selectedTextRange {
如果range.empty {
//如果选择为空,删除光标后面的字符
let start = textField。 positionFromPosition(range.start,inDirection:.Left,offset:1)
let deleteRange = textField.textRangeFromPosition(start,toPosition:range.end)
textField.replaceRange(deleteRange,withText:)
}
else {
//如果选择不为空,删除选择中的字符
textField.replaceRange(range,withText:)
}
}
}
I've spent hours trying to figure out how to create/then get a custom inputView to work.I have a grid of TextInputs (think scrabble board) that when pressed should load a custom inputView to insert text.
I've created a .xib file containing the UI elements for the custom inputView. I was able to create a CustomInputViewController and have the inputView appear but never able to get the actual TextInput to update it's value/text.
Apple documentation has seemed light on how to get this to work and the many tutorials I've have seen have been using Obj-C (which I have been unable to convert over due to small things that seem unable to now be done in swift).
What is the overarching architecture and necessary pieces of code that should be implemented to create a customInputView for multiple textInputs (delegate chain, controller, .xibs, views etc)?
Set up a nib file with the appropriate inputView layout and items. In my case I set each button to an action on File Owner of inputViewButtonPressed.
Set up a storyboard (or nib if you prefer) for a view controller.
Then using the following code, you should get what you're looking for:
class ViewController: UIViewController, UITextFieldDelegate { var myInputView : UIView! var activeTextField : UITextField? override func viewDidLoad() { super.viewDidLoad() // load input view from nib if let objects = NSBundle.mainBundle().loadNibNamed("InputView", owner: self, options: nil) { myInputView = objects[0] as UIView } // Set up all the text fields with us as the delegate and // using our input view for view in self.view.subviews { if let textField = view as? UITextField { textField.inputView = myInputView textField.delegate = self } } } func textFieldDidBeginEditing(textField: UITextField) { activeTextField = textField } func textFieldDidEndEditing(textField: UITextField) { activeTextField = nil } @IBAction func inputViewButtonPressed(button:UIButton) { // Update the text field with the text from the button pressed activeTextField?.text = button.titleLabel?.text // Close the text field activeTextField?.resignFirstResponder() } }
Alternatively, if you're wanting to be more keyboard-like, you can use this function as your action (it uses the new let syntax from Swift 1.2), break it up if you need 1.1 compatibility:
@IBAction func insertButtonText(button:UIButton) { if let textField = activeTextField, title = button.titleLabel?.text, range = textField.selectedTextRange { // Update the text field with the text from the button pressed textField.replaceRange(range, withText: title) } }
This uses the UITextInput protocol to update the text field as appropriate. Handling delete is a little more complicated, but still not too bad:
@IBAction func deleteText(button:UIButton) { if let textField = activeTextField, range = textField.selectedTextRange { if range.empty { // If the selection is empty, delete the character behind the cursor let start = textField.positionFromPosition(range.start, inDirection: .Left, offset: 1) let deleteRange = textField.textRangeFromPosition(start, toPosition: range.end) textField.replaceRange(deleteRange, withText: "") } else { // If the selection isn't empty, delete the chars in the selection textField.replaceRange(range, withText: "") } } }
这篇关于Swift中的自定义输入视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!