没有调用选择器视图功能

时间:2018-07-24 04:14:33

标签: ios swift function uipickerview

我正在制作一个带有一些文本字段的应用程序,一旦按下,该文本字段应该从底部弹出一个选择器视图,并应该显示我添加的一些数组,但是以某种方式,放置打印语句后,我发现了最重要的两个功能

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 

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

将代码放置到选择器视图中的位置的代码未运行。 我该如何解决?谢谢

import UIKit

class ViewController: UIViewController{
    var blocking = [Automation.blockTime.quarterHour, .halfHour, .hour, .twoHour]
    var blockingString = ["Quarter Hour", "Half Hour", "Hour", "Two Hours"]
    var startTimeString = ["5:00AM","6:30AM","5:30AM","6:00AM","7:00AM","7:30AM","8:00AM","8:30AM","9:00AM","9:30AM","10:00AM"]
    var endTimeString = ["5:00PM","6:30PM","5:30PM","6:00PM","7:00PM","7:30PM","8:00PM","8:30PM","9:00PM","9:30PM","10:00PM"]
    var startTime = [300, 390, 330, 360, 420, 450, 480, 510, 540, 570, 600]
    var endTime = [Int]()

    @IBOutlet weak var block: UITextField!
    @IBOutlet weak var endOfSchedule: UITextField!
    @IBOutlet weak var StartOfSchedule: UITextField!
    @IBOutlet weak var AppointmentTextField: UITextField!
    @IBOutlet weak var prorityTextField: UITextField!
    @IBOutlet weak var GoButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        createPicker()
        createToolbar()
        for EachElemnt in startTime {
            endTime.append(EachElemnt + (12 * 60))
        }
    }

    func createPicker() {
        let dayPicker = UIPickerView()
        dayPicker.delegate = self

        endOfSchedule.inputView = dayPicker
        StartOfSchedule.inputView = dayPicker
        block.inputView = dayPicker

        //Customizations
        dayPicker.backgroundColor = UIColor.darkGray
    }

    func createToolbar() {
        let toolBar = UIToolbar()
        toolBar.sizeToFit()

        //Customizations
        toolBar.barTintColor = .black
        toolBar.tintColor = .white

        let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(ViewController.dismissKeyboard))

        toolBar.setItems([doneButton], animated: false)
        toolBar.isUserInteractionEnabled = true

        prorityTextField.inputAccessoryView = toolBar
    }

    @objc func dismissKeyboard() {
        view.endEditing(true)
    }
}

extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        var returnment = Int()
        if pickerView == endOfSchedule {
            returnment = endTimeString.count
            return endTimeString.count
        }else if pickerView == StartOfSchedule {
            returnment = startTimeString.count
            return startTimeString.count
        }else if pickerView == block {
            returnment = blocking.count
            return blocking.count
        }

        return returnment

        func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

            var returnment = String()
            if pickerView == endOfSchedule {
                returnment = endTimeString[row]

            }else if pickerView == StartOfSchedule {
                returnment = startTimeString[row]

            }else if pickerView == block {
                returnment = blockingString[row]

            }
            print(returnment)
            return returnment
        }

        func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            if pickerView == endOfSchedule {
                endOfSchedule.text = endTimeString[row]
            }else if pickerView == StartOfSchedule {
                StartOfSchedule.text = startTimeString[row]
            }else if pickerView == block {
                block.text = blockingString[row]
            }
        }

        func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
            var label: UILabel

            if let view = view as? UILabel {
                label = view
            } else {
                label = UILabel()
            }

            label.textColor = .white
            label.textAlignment = .center
            label.font = UIFont(name: "Menlo-Regular", size: 17)
            if pickerView == endOfSchedule {
                label.text = endOfSchedule.text
            }else if pickerView == StartOfSchedule {
                label.text = StartOfSchedule.text
            }else if pickerView == block {
                label.text = block.text
            }

            return label
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您有几个问题。

  1. 您从未设置选择器的dataSource。添加dayPicker.dataSource = self
  2. 您已经实现了titleForRowviewForRow。仅实现其中之一。
  3. 您的if语句都不会成功,因为您正在将UIPickerView变量与UITextField实例进行比较。我很惊讶您的代码甚至可以编译。

要解决最后一个问题,您需要设置所有文本字段的委托。然后实现textFieldDidBeginEditing委托方法。在那,将属性设置为文本字段。然后,在所有选择器视图方法中,将该文本字段属性与文本字段出口进行比较,以便知道哪个文本字段是当前文本字段。或检查每个文本字段是否是第一响应者。

示例:

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if endOfSchedule.isFirstResponder {
        return endTimeString.count
    } else if StartOfSchedule.isFirstResponder {
        return startTimeString.count
    } else {
        return blocking.count
    }
}

最后,请确保您没有将其中一些方法放入另一个方法中。您发布的代码在错误的位置带有花括号。

答案 1 :(得分:0)

titleForRow:未得到调用,因为您还使用了viewForRow:方法,该方法根本不可能包含在同一委托中。您只需使用其中之一。

解决方案:在viewForRow:中,将label.text设置为要在titleForRow:中返回的字符串,并删除titleForRow:方法。

答案 2 :(得分:-1)

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

    if pickerView == endOfSchedule {

        return endTimeString.count
    }else if pickerView == StartOfSchedule {

        return startTimeString.count
    }else {

        return blocking.count
    }
}                                                                             

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if pickerView == endOfSchedule {
        return endTimeString[row]

    }else if pickerView == StartOfSchedule {
        return startTimeString[row]

    }else  {
        return blockingString[row]
    }
}

尝试返回每种情况,然后尝试对其进行调试。也许它将起作用,并且似乎您错过了dataSource。