2

I'm working on my first Swift ios application.

Can't get data from pickerData into my picker, but I only get question marks instead of real values. I guess it's something to do with delegate, but not sure what wrong.

import UIKit
import CoreData

class NewWorkoutController: UIViewController,UIPickerViewDelegate, UIPickerViewDataSource {

@IBOutlet weak var workoutDistance: UITextField!

let pickerData = ["11","12","13","14","15"]

// Data Sources
func numberOfComponentsInPickerView(distancePickerView: UIPickerView) -> Int {
    return 1
}
func pickerView(distancePickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerData.count
}
// Delegates
func pickerViewReturnRow(distancePickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    return pickerData[row]
}
func pickerViewText(distancePickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    workoutDistance.text = pickerData[row]
}
func doneDistancePicker() {
    workoutDistance.resignFirstResponder()
}
func cancelDistancePicker() {
    workoutDistance.resignFirstResponder()
}

@IBAction func textFieldDistanceEditing2(sender: UITextField) {

    // Create picker view
    var distancePickerView: UIPickerView
    distancePickerView = UIPickerView(frame: CGRectMake(0, 200, view.frame.width, 300))
    distancePickerView.backgroundColor = .whiteColor()

    distancePickerView.showsSelectionIndicator = true
    distancePickerView.delegate = self
    distancePickerView.dataSource = self

    // Create toolbar
    var toolBar = UIToolbar()
    toolBar.barStyle = UIBarStyle.Default
    toolBar.translucent = true
    toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
    toolBar.sizeToFit()

    // Create buttons
    var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "doneDistancePicker")
    var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
    var cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelDistancePicker")

    // Assign buttons to toolbar
    toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
    toolBar.userInteractionEnabled = true

    // Add pickerview and toolbar to textfield
    workoutDistance.inputView = distancePickerView
    workoutDistance.inputAccessoryView = toolBar

    sender.inputView = distancePickerView
}

override func viewDidLoad() {
    super.viewDidLoad()
}
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Nata_Codes
  • 23
  • 3

2 Answers2

5

The signature for this function is wrong:

func pickerViewReturnRow(distancePickerView: UIPickerView, 
    titleForRow row: Int, 
    forComponent component: Int) -> String!

It should be

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

As a result that method (which provides titles for the rows in your picker) isn't getting called.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Ah-oh, ReturnRow was for cell, bad copy-paste :( Thanks a lot! – Nata_Codes Apr 22 '15 at 23:59
  • One problem with optional delegate methods is that if you get the method name wrong, the object silently doesn't call the method. That can make debugging hard. Always remember to check your method names, and then set a breakpoint (or insert a println) and make sure your method is really being called. – Duncan C Apr 23 '15 at 00:05
  • 1
    Auto-complete can be a helpful crutch in getting the method names right though. As soon as you declare your class as conforming to the protocol, all the methods for the protocol will try to auto-complete, even the optional ones. – nhgrif Apr 23 '15 at 00:26
0

In all pickerView delegate method's signature, you replaced pickerView with your pickerView outlet that is distancePickerView which is not required. You should modify code inside delegate method's body because delegate methods are automatically called by swift compiler if we confirm your pickerView delegate and datasource to your ViewController class by writing code as:

pickerViewOutlet.delegate = self
pickerViewOutlet.dataSource = self

So just modify all pickerView delegate methods with its default signature then it will get called by compiler and data will display in it.

Ashvini
  • 342
  • 3
  • 11