UIPickerView未按预期显示

时间:2016-10-11 16:31:12

标签: ios swift xcode uipickerview

我在一个ViewController中有多个UIPickerViews。我相信我已正确设置数据源和委托,但是,当我运行代码时,PickerViews没有填充。我得到的是"()"显示在所有PickerViews中。

任何人都知道我做错了什么?每个我看过/ googled intimates的地方我都正确设置了这个。

的XCode:

enter image description here

模拟器:

enter image description here

class GroundViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {

 let actionRatings = ["0","1","2","3","4","5"]
 let hogLevels = ["0","1","2","3","4"]
 let defTerrainTitles = ["Open", "Close", "Very Close", "Extr. CLose"]

 var attackerAR = 0

 @IBOutlet var attARPicker: UIPickerView!
 @IBOutlet var attCombatFactorsInput: UITextField!
 @IBOutlet var bonzaiLabel: UILabel!
 @IBOutlet var bonzaiSwitch: UISwitch!
 @IBOutlet var overrunSwitch: UISwitch!

 @IBOutlet var defARPicker: UIPickerView!
 @IBOutlet var defCombatFactorsInput: UITextField!
 @IBOutlet var defHogLevel: UIPickerView!
 @IBOutlet var defTerrain: UIPickerView!

 @IBOutlet var lblSurpiseResults: UILabel!
 @IBOutlet var lblAttackerResults: UILabel!
 @IBOutlet var lblDefenderResults: UILabel!

 @IBOutlet var lblShowDice: UILabel!

 @IBAction func btnResolveCombat(sender: UIButton) {
      //collect data from fields and output results
 }

 @IBAction func btnRest(sender: UIButton) {
      //reset all components
 }

 override func viewDidLoad() {
      super.viewDidLoad()

      //set delegates & datasources
      attARPicker.dataSource = self
      attARPicker.delegate = self
      defARPicker.dataSource = self
      defARPicker.delegate = self
      defTerrain.dataSource = self
      defTerrain.delegate = self
      defHogLevel.dataSource = self
      defHogLevel.delegate = self


      //check appSettings
      bonzaiSwitch.setOn(false, animated: true)
      if burmaSetting == false {
           bonzaiLabel.hidden = true
           bonzaiSwitch.hidden = true
      }
      if diceSetting == false {
           lblShowDice.hidden = true
      }

      // Do any additional setup after loading the view, typically from a nib.
 }

 override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
 }


 //MARK: - Delegates and data sources

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


 //MARK: Data Sources

 func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
      //set per pickerView.tag
      /* 
         tag 1 = attARPicker
         tag 2 = defARPicker
         tag 3 = defHogLevel
         tag 4 = defTerrain
      */

      if (pickerView.tag == 1){
           return actionRatings.count
      } else if (pickerView.tag == 2) {
           return actionRatings.count
      } else if (pickerView.tag == 3){
           return hogLevels.count
      } else {
           return defTerrainTitles.count
      }

      /*
      switch pickerView.tag {
           case 1, 2:
                return actionRatings.count
           case 3:
                return hogLevels.count
           case 4:
                return defTerrainTitles.count
           default:
                return 0
      }
      */
 }

 //MARK: Delegates

 func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
      //set per pickerview.tag
      /*func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
                if (pickerView.tag == 1){
                     return "\(picker1Options[row])"
                }else{
                     return "\(picker2Options[row])"
                }*/

      if (pickerView.tag == 1){
           return actionRatings[row]
      } else if (pickerView.tag == 2) {
           return actionRatings[row]
      } else if (pickerView.tag == 3){
           return hogLevels[row]
      } else {
           return defTerrainTitles[row]
      }

 }

 func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
      let titleData = actionRatings[row]
      let myTitle = NSAttributedString(string: String(titleData), attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 12.0)!,NSForegroundColorAttributeName:UIColor.blueColor()])
      return myTitle
 }}

0 个答案:

没有答案