skip to Main Content

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 g : String = ""
    var gr : String = ""
    var mean : Double = 0.0
    
    
    
    
    @IBOutlet weak var t2: UITextField!
    
    @IBOutlet weak var s1: 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
           
            
            t1.text = nil
            t2.text = nil
            
            
        }else{
            s1.text = "None"
        
        }
        
        }

            
    @IBAction func calc(_ sender: Any) {
        for (key, value) in student{
            theSum = theSum + value
            if count == 0
            {
                maxName = key
                min = value
            }
            
            count = count + 1
            
       
    
            
        }
        
     mean = Double(theSum)/Double(student.count)
    
            let grade1 = min
            switch grade1
        {
            case 93...100: g = "A"
           
            case 0...59: g = "F"
            default: g = ""
                
            }
        
        
            
            
        }
    }
    
    
    
    
    
    
    

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

3

Answers


  1. 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.

    Login or Signup to reply.
  2. there is a lot of method to do this 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
    
    }
    

    }

    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search