Nata_Codes
Nata_Codes

Reputation: 23

First iOS app in Swift - Question marks in UIPickerView

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()
}
}

Upvotes: 2

Views: 1192

Answers (2)

Ashvini
Ashvini

Reputation: 352

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.

Upvotes: 0

Duncan C
Duncan C

Reputation: 131491

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.

Upvotes: 5

Related Questions