Swift中的自定义输入视图

我花了好几个小时试图弄清楚如何创建/然后让自定义inputView工作。 我有一个TextInputs网格(想想拼字游戏板)按下时应该加载自定义inputView来插入文本。

我创建了一个.xib文件,其中包含自定义inputViewUI elements 。 我能够创建一个CustomInputViewController并显示inputView但永远无法获得实际的TextInput来更新它的值/文本

Apple文档似乎很清楚如何让它工作,我见过的许多教程都使用了Obj-C(由于现在似乎无法在swift中完成的小东西,我无法转换) 。

为多个textInputs (委托链,控制器,.xibs,视图等)创建customInputView应该实现的总体架构和必要的代码片段是什么?

你不应该经历所有那些麻烦。 在iOS 8中有一个名为UIAlertController的新类,您可以在其中添加TextField以供用户输入数据。 您可以将其设置为Alert或ActionSheet的样式。

例:

 let alertAnswer = UIAlertController(title: "Input your scrabble Answer", message: nil, preferredStyle: .Alert) // or .ActionSheet 

现在你有控制器,添加字段:

 alertAnswer.addTextFieldWithConfigurationHandler { (get) -> Void in getAnswer.placeholder = "Answer" getAnswer.keyboardType = .Default getAnswer.clearsOnBeginEditing = true getAnswer.borderStyle = .RoundedRect } // add as many fields as you want with custom tweaks 

添加操作按钮:

 let submitAnswer = UIAlertAction(title: "Submit", style: .Default, handler: nil) let cancel = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil) alertAnswer.addAction(submitAnswer) alertAnswer.addAction(cancel) 

随时出现控制器:

 self.presentViewController(alertAnswer, animated: true, completion: nil) 

如您所见,您可以使用各种处理程序在任何步骤传递自定义代码。

例如,它的外观如下: 它看起来如何的一个例子

使用适当的inputView布局和项设置nib文件。 在我的情况下,我将每个按钮设置为inputViewButtonPressed文件所有者上的inputViewButtonPressed

为视图控制器设置故事板(或者如果您愿意,可以使用nib)。

然后使用以下代码,您应该得到您正在寻找的东西:

 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() } } 

或者,如果您想要更像键盘,可以使用此function作为您的操作(它使用Swift 1.2中的新let语法),如果您需要1.1兼容性,请将其分解:

  @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) } } 

这使用UITextInput协议来适当地更新文本字段。 处理删除有点复杂,但仍然不是太糟糕:

  @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: "") } } }