Home > Net >  swift. How can I pass the calculation result to another screen?
swift. How can I pass the calculation result to another screen?

Time:12-16

I am swift beginner. In this project I need to store user's input into dictionary. Then I use the data to do some calculation. I have a "calculate" button, when pressed, the program begins to calculate results and passes the result to the other screen. I am stuck here just can't get the data passed . Maybe there is something wrong with my "calculation" code. Please help.


import UIKit

class ViewController: UIViewController {

    
    var student: [String: Int] = [:]
    
    var theSum = 0
    var max = 0
    var min = 0
    var maxName = ""
    var minName = ""
    var count = 0
    var g : String = ""
    var gr : String = ""
    var mean : Double = 0.0
    
    
    
    @IBOutlet weak var t1: UITextField!
    @IBOutlet weak var t2: UITextField!
    
    @IBOutlet weak var s1: UILabel!
    @IBOutlet weak var s2: UILabel!
    @IBOutlet weak var s3: UILabel!
    
    


    
    
    

    @IBAction func add(_ sender: UIButton) {
        
       
        let getInput1 = t1.text!
        let getInput2 = Int(t2.text!)
    
        
        if getInput1 != nil && getInput2! >= 0
        {
            student[getInput1] = getInput2
            s1.text = getInput1
            s2.text = String(getInput2!)
            s3.text = "Succeed"
            
            t1.text = nil
            t2.text = nil
            
            
        }else{
            s1.text = "None"
            s2.text = "None"
            s3.text = "Failed"
        }
        
        }

            
    @IBAction func calc(_ sender: Any) {
        for (key, value) in student{
            theSum = theSum   value
            if count == 0
            {
                maxName = key
                min = value
            }
            
            count = count   1
            
        
        if min > value {
            maxName = key
            min = value
        }
    
    if max < value{
        minName = key
        max = value
    }
    
            
        }
        
     mean = Double(theSum)/Double(student.count)
    
            let grade1 = min
            switch grade1
        {
            case 93...100: g = "A"
            case 85...92: g = "B"
            case 77...84: g = "C"
            case 69...76: g = "D"
            case 60...68: g = "E"
            case 0...59: g = "F"
            default: g = ""
                
            }
        
        let grade2 = max
        switch grade2
    {
        case 93...100: gr = "A"
        case 85...92: gr = "B"
        case 77...84: gr = "C"
        case 69...76: gr = "D"
        case 60...68: gr = "E"
        case 0...59: gr = "F"
        default: gr = ""
            
        }
    }
    
         
    
    
    
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destination = segue.destination as?
            ResultsViewController{
            destination.maxN = maxName
            destination.ma = max
            destination.minN = minName
            destination.mi = min
            destination.avrg = mean
            destination.g1 = g
            destination.g2 = gr
            
            
        }
    }
    
    
    
    
    
    
    

    override func viewDidLoad() {
            super.viewDidLoad()
        
    // Do any additional setup after loading the view.
        s1.text = "Name"
        s2.text = "Score"
      
    }


}
import UIKit

class ResultsViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        maxName.text = maxN
        max.text = String(ma)
        minName.text = minN
        min.text = String(mi)
        avg.text = String(avrg)
        grade1.text = g1
        grade2.text = g2
        // Do any additional setup after loading the view.
    }
    
    
    
    @IBOutlet weak var maxName: UILabel!
    @IBOutlet weak var max: UILabel!
    
    @IBOutlet weak var minName: UILabel!
    @IBOutlet weak var min: UILabel!
    
    @IBOutlet weak var avg: UILabel!
    
    @IBOutlet weak var grade1: UILabel!
    @IBOutlet weak var grade2: UILabel!
    
    var maxN : String = ""
    var ma : Int = 0
    var minN : String = ""
    var mi : Int = 0
    var avrg : Double = 0.0
    var g1 : String = ""
    var g2 : String = ""
    
    
    
}

App picture

CodePudding user response:

You never call performSegue(withIdentifier identifier: String, sender: Any?). I assume that you want to perform the segue after the calculation is finished, right? That's when you should call performSegue. Don't forget to give your segue an ID inside your storyboard and hand this ID as the identifier to the performSegue function.

CodePudding user response:

You have some bugs in your UIViewController

in your

@IBAction func add(_ sender: UIButton) 

instead of

if getInput1 != nil && getInput2! >= 0

you should go for

if t1.hasText && t2.hasText && getInput2! >= 0

and for passing values to ResultsViewController

  • Add segue from the storyboard and name the Identifier "SomeID"

  • Then go to the UIViewcontroller class and add

    self.performSegue(withIdentifier: "SomeID", sender: self)

to your calc func.

Full Code = https://github.com/alirzaeram/Pass-the-calculation-result

CodePudding user response:

there is a lot of method to do this but you can use UserDefaults

//to save value you can use UserDefaults.standard.setValue(Any?, forKey: String)

//and to get let yourVar = UserDefaults.standard.value(forKey: String) 0r//

class FirstViewController: UIViewController {

var yourCalculation = "your calculation"
var yourResult = 5


override func viewDidLoad() {
    super.viewDidLoad()
    
}

@IBAction func btn(){
    let vc = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
    vc.yourCalculation = yourCalculation
    vc.yourResult = yourResult
    self.navigationController?.pushViewController(vc, animated: true)
}

}

//your second viewController class SecondViewController: UIViewController {

@IBOutlet weak var yourLabel:UILabel!
@IBOutlet weak var yourLabel2:UILabel!

var yourCalculation = String()
var yourResult = Int()


override func viewDidLoad() {
    super.viewDidLoad()
    self.yourLabel.text = yourCalculation
    let yourInt : String = String(yourResult)
    self.yourLabel2.text = yourInt

}

}

  • Related