快速搜寻不起作用(初学者)

时间:2018-07-25 15:12:20

标签: swift segue

我想将数据从我的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.
}    
}

1 个答案:

答案 0 :(得分:0)

您在连接@IBAction

时出错

您应该将button连接到ViewController,但是将barButton item连接到ViewController;)

  • Mistake:

enter image description here

  • 您应该做什么:

enter image description here

我建议您始终定义操作Event;)