繁体   English   中英

Swift中的自定义输入视图

[英]Custom Input View in Swift

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

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

苹果文档似乎很清楚如何使这个工作,我已经看到的许多教程一直在使用Obj-C(由于现在似乎无法在swift中完成的小事情,我无法转换) 。

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

使用适当的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()
    }
}

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM