Swift 中的第一个 iOS 应用程序 - UIPickerView 中的问号

First iOS app in Swift - Question marks in UIPickerView

我正在处理我的第一个 Swift ios 应用程序。

无法将数据从 pickerData 获取到我的选择器中,但我只得到问号而不是实际值。我想这与委托有关,但不确定哪里出了问题。

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

此函数的签名错误:

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

应该是

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

因此,该方法(为您的选择器中的行提供标题)未被调用。

在所有 pickerView 委托方法的签名中,您将 pickerView 替换为您的 pickerView 插座,即 distancePickerView,这不是必需的。您应该修改委托方法主体内的代码,因为如果我们确认您的 pickerView delegate 和 [=,swift 编译器会自动调用委托方法15=]datasource 到你的 ViewController class 通过编写代码如下:

pickerViewOutlet.delegate = self
pickerViewOutlet.dataSource = self

因此只需使用其默认签名修改所有 pickerView 委托方法,然后它就会被编译器调用并在其中显示数据。