我想将数据从我的TypeController
传递到ViewController
。在TypeController中,从PickerView
中选择一个项目,然后将其传递给typeLabel。到目前为止,一切正常,现在,操作“ SaveDate”(单击界面上的“完成”按钮)应该将数据传递到ViewController
。
我对另一个ViewController
使用了相同的(调整后的)代码,但它带有日期选择器而不是pickerview,并且运行良好。
花了一天的时间进行谷歌搜索和尝试,但是我不知道问题出在哪里,因此我们将不胜感激。
谢谢
这是我的TypeController.swift
import UIKit
class TypeController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
// Outlets
@IBOutlet weak var typeLabel: UILabel!
@IBOutlet weak var pickerView: UIPickerView!
// Actions
// Click on 'Cancel' sends back to Main View
@IBAction func ToHome(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
// send Date through Segue to Viewcontroller Screen
@IBAction func SaveDate(_ sender: Any) {
performSegue(withIdentifier: "TypeSegueBack", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
let viewController2 = segue.destination as! ViewController
viewController2.passType = typeLabel.text!
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
// Protocol implementation
func transmisie() -> String {
return typeLabel.text!
}
// Setup PickerView
let types = ["a", "b", "m", "q", "y"]
// How many rows?
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
// Text displayed in selection --> Zugriff auf Array
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return types[row]
}
// Number of items = length of array
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return types.count
}
// Text for Label from Pickerview
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
typeLabel.text = types[row]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}