私は最初の 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()
}
}