Swift中的自定义输入视图

时间:2014-10-28 05:13:39

标签: ios cocoa-touch swift uitextinput uiinputviewcontroller

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

我已为自定义UI elements创建了包含inputView .xib 文件。我能够创建CustomInputViewController并显示inputView但却无法让实际的TextInput更新值/文字

Apple文档似乎很清楚如何使其工作,我已经看到的许多教程一直在使用Obj-C(由于现在似乎无法做到的小事,我无法转换它们在swift完成。

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

2 个答案:

答案 0 :(得分:9)

使用适当的inputView布局和项目设置nib文件。在我的情况下,我将每个按钮设置为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()
    }
}

或者,如果您想要更像键盘,可以使用此功能作为您的操作(它使用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: "")
            }
        }
    }

答案 1 :(得分:7)

你不应该经历所有那些麻烦。 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)

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

例如,它的外观如下: An example of how it could look