Search code examples
swiftsegueuipickerview

UIPickerView sending data without segue


I´m working on a project that calculates your points you need for school just by you writing your grade in all subjects.

I use this UIPickerView for the user to tell the program what it´s grade is. Then I save it in a variable. But I can´t send that variable to another viewcontroller without a segue and I would like to avoid using segues. How do I proceed?

This is my Viewcontroller for one subjekt:

(Some of it is in swedish)

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

Solution

  • What is wrong with using segues? Thats the preferred method of doing it for a parent-child relationship. Other methods are:

    1. Broadcast a notification with NotificationCenter.default
    2. Use a shared datastore or singleton to house the values (Realm for instance)
    3. If you are instantiating the VC manually with no segue, you pass in the data after your create it but before you push it on to the stack or make it the root view controller.