如何在swift

时间:2016-07-12 05:12:16

标签: ios swift uidatepicker

我想知道如何在swift

中将间隔值添加到UIDateTimePicker
8am-8:30am
8:30am-9am
9am-9:30am
9:30am-10am
10am-10:30am
10:30am-11am

1 个答案:

答案 0 :(得分:1)

你可以尝试这样的事情。在UIPickerViewxib storyboard之后添加一个viewController,然后将delegatedatasoucre设置为UIPickerView viewController。现在创建一个这样的字符串数组。

 var timeArr: [String] = [String]()

现在在viewDidLoad中初始化此数组

override func viewDidLoad() {
    super.viewDidLoad()
    self.timeArr = ["8:00AM - 8:30AM", "8:30AM - 9:00AM", "9:00AM - 9:30AM", "9:30AM - 10:00AM"]
}

现在在您的delegate中添加datasourceUIPicker ViewController方法

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
     return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return self.timeArr.count
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return self.timeArr[row]
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    print(self.timeArr[row])
}

这将创建您想要选择时间的Picker。