UIPicker作为UITextfield键盘的输入

时间:2015-03-16 20:58:44

标签: ios uitableview swift uitextfield uipickerview

最初我想在选择UITableViewCell或UILabel时实现一个选择器。

最好的方法是 1.子类UItextfield并将其inputView设置为选择器。 2.使该文本字段成为didSelectRowAtIndexPath

中的FirstResponder

下面的答案

2 个答案:

答案 0 :(得分:0)

inputView属性是将选择器视图分配给TextField的第一响应者。它不会显示PickerView。因此,WarmUpTextField.inputView = Picker应移至viewDidLoad()

您在didSelectRowAtIndexPath中的期望是将TextField设置为第一响应者。此功能的下面代码应该按预期工作。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        if indexPath.section == 0 {

            NameTexField.becomeFirstResponder()

        } else if indexPath.section == 1 {

            WarmUpTextField.becomeFirstResponder()

        }
    }

答案 1 :(得分:0)

最好的办法是将UITextfield子类化并执行以下操作:

class PickerTextField: UITextField, UIPickerViewDataSource, UIPickerViewDelegate {

    var picker:UIPickerView = CustomUIPickerView()

    var pickerHours: Int = 0
    var pickerMinutes: Int = 0
    var pickerSeconds: Int = 0
    var pickerTotal: Int = 0

    override func awakeFromNib() {
        super.awakeFromNib()

        picker.delegate = self
        picker.dataSource = self

    }

    override func layoutSubviews() {

        super.layoutSubviews()

        (pickerHours,pickerMinutes,pickerSeconds) = MakeTimeFromString(self.text!)

        self.inputView = configurePicker()
        self.inputAccessoryView = configureAccessoryView()


    }

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

        // Prevent textfield from editing
        return false

    }

    func configurePicker() -> UIView {

        picker.selectRow(pickerHours, inComponent: 0, animated: true)
        picker.selectRow(pickerMinutes, inComponent: 1, animated: true)
        picker.selectRow(pickerSeconds, inComponent: 2, animated: true)

        return picker

    }

    func configureAccessoryView() -> UIView {

        let inputAccessoryView = UIToolbar(frame: CGRectMake(0.0, 0.0, UIScreen.mainScreen().bounds.size.width, 44))
        inputAccessoryView.barStyle = UIBarStyle.BlackTranslucent

        let flex = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil)

        // Configure done button
        let doneButton = UIBarButtonItem()
        doneButton.title = "Done"
        doneButton.tintColor = UIColor.greenColor()
        doneButton.action = Selector("dismissPicker")

        inputAccessoryView.items = NSArray(array: [flex, doneButton]) as? [UIBarButtonItem]

        return inputAccessoryView
    }

    // Disallow selection or editing and remove caret

    override func caretRectForPosition(position: UITextPosition) -> CGRect {
        return CGRectZero
    }

    override func selectionRectsForRange(range: UITextRange) -> [AnyObject] {
        return []
    }

    override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {

        UIMenuController.sharedMenuController().menuVisible = false

        if action == "copy:" || action == "selectAll:" || action == "paste:" {
            return false
        }

        return super.canPerformAction(action, withSender:sender)
    }

    func dismissPicker () {

        self.resignFirstResponder()
    }

    //MARK: - UIPickerView Functions

    // returns the number of 'columns' to display.
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {

        return 3
    }

    // returns the # of rows in each component..
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        if component == 0 {

            return 24

        }

        return 60
    }

    func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {

        let title = String(row)
        let attributedString = NSAttributedString(string: title, attributes: [NSForegroundColorAttributeName: UIColor.whiteColor()])

        pickerView.backgroundColor = UIColor.clearColor()

        return attributedString
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        if component == 0 {

            pickerHours = row

        } else if component == 1 {

            pickerMinutes = row

        } else if component == 2 {

            pickerSeconds = row
        }

        pickerTotal = AssembleTimeFromComponents(pickerHours, minutesComponent: pickerMinutes, secondsComponent: pickerSeconds)

        UpdateLabel()

    }

    func UpdateLabel() {

        self.text = MakeTimeLabel(pickerTotal, type: "Routine")
        self.sizeToFit()

    }
}

对于更通用的库,请尝试: https://github.com/gtsif21/UIKeyboardLikePickerTextField