6

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

我创建了一个.xib文件,其中包含UI elements自定义.xib 文件inputView。我能够创建一个CustomInputViewControllerinputView出现,但永远无法获得实际TextInput更新它的value/text

Apple 文档似乎很清楚如何让它工作,而且我见过的许多教程都在使用 Obj-C(由于现在似乎无法快速完成的小事情,我无法转换它) .

应该实现哪些总体架构和必要的代码片段来创建一个customInputViewfor multiple textInputs(委托链、控制器、.xib、视图等)?

4

2 回答 2

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: "")
            }
        }
    }
于 2015-02-17T17:23:34.620 回答
7

你不应该经历所有的麻烦。iOS 8 中有一个新的类叫做:UIAlertController你可以在其中添加 TextFields 供用户输入数据。您可以将其设置为警报或操作表。

例子:

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)

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

例如,这就是它的外观: 它的外观示例

于 2015-02-15T10:34:42.550 回答