自定义PickerView iOS

时间:2015-12-19 09:25:01

标签: ios swift

我已经实现了PickerView,但我想像闹钟应用程序一样自定义它。

enter image description here

您可以看到每个组件行都有“小时”或“分钟”。我想把它设置为“小时”和“分钟”站在这里,并且每个未选择的元素,它们没有“小时”或“分钟”,只选择一个有“小时”或“分钟”的元素。 怎么做?

这是我的代码:

import UIKit

class StoryAddViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

  @IBOutlet var titleTextField: TextField!
  @IBOutlet var hashtagsTextField: TextField!
  @IBOutlet var createButton: UIButton!
  @IBOutlet var timePickerView: UIPickerView!
  var timePickerDataBase = [ ["1", "2" , "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24"], ["1", "2" , "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26" , "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59"] ]
  var hour = String()
  var minute = String()

  var story: Story?

  override func viewDidLoad() {
    super.viewDidLoad()
    title = "Add new story"
    view.backgroundColor = Colors.primaryBackgroundColor
    (tabBarController as! SLTabBarController).toggleTabBar(animated: false, showed: false)

    createButton.backgroundColor = Colors.tintColor
    createButton.tintColor = UIColor.whiteColor()
  }

  override func viewWillAppear(animated: Bool) {
    if story == nil {
      story = Story.create(getDatabase())
    }

    titleTextField.text = story?.title
    hashtagsTextField.text = story?.hashtags.joinWithSeparator(", ")
    super.viewWillAppear(animated)
  }

  override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    (tabBarController as! SLTabBarController).toggleTabBar(animated: true, showed: true)
  }

  @IBAction func handleViewTap(sender: AnyObject) {
    view.endEditing(true)
  }

  @IBAction func handleCreateButtonTap(sender: UIButton) {
    story?.title = titleTextField.text!
    story?.hashtags = hashtagsTextField.text!.split("\\s*,\\s*")

    try! story?.save()
    navigationController?.popViewControllerAnimated(true)
  }

// MARK: handle picker view

  func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return timePickerDataBase.count
  }

  func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return timePickerDataBase[component].count
  }

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

  func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
    let pickerLabel = UILabel()
    pickerLabel.textColor = UIColor.blueColor()
    if component == 0 {
      pickerLabel.text = timePickerDataBase[component][row] + "   Hour"
      pickerLabel.font = UIFont(name: "Arial-BoldMT", size: 25)
      pickerLabel.textAlignment = NSTextAlignment.Center
    } else {
      pickerLabel.text = timePickerDataBase[component][row] + "   Minute "
      pickerLabel.font = UIFont(name: "Arial-BoldMT", size: 25)
      pickerLabel.textAlignment = NSTextAlignment.Center
    }
    return pickerLabel
  }

  func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
    return 140
  }

  func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    print(component)
    print(row)
    switch component {
    case 0:
      hour = timePickerDataBase[component][row]
      print(hour)
    case 1:
      hour = timePickerDataBase[component][row]
      print(minute)
    default:
      break
    }
  }
}

1 个答案:

答案 0 :(得分:0)

我不建议以我认为您尝试做的方式动态更改字段的值(这看起来很奇怪),但如果您真的不得不这样做,则应重新加载< / p>

    this.getField("TOTAL").value =
  this.getField("P6eval_competences.manageriales.0").value +
  this.getField("P6eval_competences.manageriales.1").value +
  this.getField("P6eval_competences.manageriales.2").value +
  this.getField("P6eval_competences.manageriales.3").value +
  this.getField("P6eval_competences.manageriales.4").value +
  this.getField("P6eval_competences.manageriales.5").value +
  this.getField("P6eval_competences.manageriales.6").value +
  this.getField("P6eval_competences.manageriales.7").value +
  this.getField("P6eval_competences.manageriales.8").value +
  this.getField("P6eval_competences.manageriales.9").value +
  this.getField("P6eval_competences.manageriales.10").value +  
  this.getField("P6eval_competences.manageriales.11").value +
  this.getField("P6eval_competences.manageriales.12").value +
  this.getField("P6eval_competences.manageriales.13").value +
  this.getField("P6eval_competences.manageriales.14").value +
  this.getField("P6eval_competences.manageriales.15").value +
  this.getField("P6eval_competences.manageriales.16").value +
  this.getField("P6eval_competences.manageriales.17").value +
  this.getField("P6eval_competences.manageriales.18").value +
  this.getField("P6eval_competences.manageriales.19").value +
  this.getField("P6eval_competences.manageriales.20").value +
  this.getField("P6eval_competences.manageriales.21").value +
  this.getField("P6eval_competences.manageriales.22").value +  
  this.getField("P6eval_competences.manageriales.23").value +
  this.getField("P6eval_competences.manageriales.24").value +
  this.getField("P6eval_competences.manageriales.25").value +
  this.getField("P6eval_competences.manageriales.26").value +
  this.getField("P6eval_competences.manageriales.27").value +
  this.getField("P6eval_competences.manageriales.28").value +
  this.getField("P6eval_competences.manageriales.29").value +
  this.getField("P6eval_competences.manageriales.30").value +
  this.getField("P6eval_competences.manageriales.31").value;

if (getField("TOTAL").value == "21") {
  var expTplt = getTemplate("NIVEAU MATURE");
  expTplt.spawn(numPages, true, false);
}