UIPickerView在没有segue的情况下发送数据

时间:2017-01-17 19:07:54

标签: swift segue uipickerview

我正在开展一个项目,通过你在所有科目上写下你的成绩来计算你所需要的学分。

我使用这个UIPickerView让用户告诉程序它的等级是什么。然后我将它保存在变量中。但我不能将该变量发送到另一个没有segue的viewcontroller,我想避免使用segues。我该怎么办?

这是我的一个主题的Viewcontroller:

(其中一些是瑞典语)

import UIKit

class Q_A_Religion: UIViewController, UIPickerViewDataSource,  UIPickerViewDelegate {


@IBOutlet weak var label: UILabel!
@IBOutlet weak var label2: UILabel!
@IBOutlet weak var picker: UIPickerView!


let A: Double = 20.0
let B: Double = 17.5
let C: Double = 15.0
let D: Double = 12.5
let E: Double = 10.0
let F: Double = 0.0



var totaltmeritvärdeIReligion: Double = 0
 //förvarar värdet av betyget som man skrivit in

let betygsAltenativ = [" ","A", "B", "C", "D", "E", "F", "-"]


public func numberOfComponents(in pickerView: UIPickerView) -> Int
{
    return 1
}

public func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
    return betygsAltenativ[row]
}

public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
    return betygsAltenativ.count
}

public func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{

    let BetygValt = betygsAltenativ[row]
    label.text = betygsAltenativ[row]


    switch (BetygValt)

    {


    case " ":
        label2.text = (" ")
        break


    case "A":
        label2.text = ("+ 20p")
        totaltmeritvärdeIReligion = Double(A)

    case "B":
        label2.text = ("+ 17.5p")
        totaltmeritvärdeIReligion = Double(B)
    case "C":
        label2.text = ("+ 15p")
        totaltmeritvärdeIReligion = Double(C)

    case "D":
        label2.text = ("+ 12.5p")
        totaltmeritvärdeIReligion = Double(D)

    case "E":
        label2.text = ("+ 10p")
        totaltmeritvärdeIReligion = Double(E)

    case "F":
        label2.text = ("+ 0p")
        totaltmeritvärdeIReligion = Double(F)

    case "-":
        label2.text = ("+ 0p")


    default:
        print("Inget betyg valt")

    }//switch slutar

    func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let nextVC: Totaltmeritva_rde = segue.destination as! Totaltmeritva_rde

        nextVC.Religionbetygetskickat = totaltmeritvärdeIReligion

    }

}// func pickerView slutar



}//Slutet av allt

1 个答案:

答案 0 :(得分:2)

使用segues有什么问题?这是为亲子关系做首选的方法。其他方法是:

  1. 使用NotificationCenter.default
  2. 广播通知
  3. 使用共享数据存储区或单例来存放值(Realm for instance
  4. 如果您在没有segue的情况下手动实例化VC,则在创建数据之后但在将数据推送到堆栈之前传入数据或将其作为根视图控制器。