0

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()
        
   
      
    }
Spiky
  • 5
  • 3

3 Answers3

1

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

Alirza Eram
  • 134
  • 11
  • Hi. I got this message " this class is not key value coding-compliant for the key grade". when I press the "calculate" button. What problem it could be? – Spiky Dec 17 '21 at 01:48
  • I think it's because of the width constraint of calculate button, remove it. – Alirza Eram Dec 17 '21 at 07:21
0

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.

nbtown
  • 36
  • 5
  • Yes, I want get the calculation done at this screen and just pass the result to UILabel on another screen. Do I need a addtion button to perform segue or just one button to perform both calculation and segue? – Spiky Dec 16 '21 at 05:42
  • No, just call performSegue wherever you want and it will go to the new screen. Try calling it after your switch grade2 statement. – nbtown Dec 16 '21 at 05:58
  • I use the prepareForSegue:sender: , I think if I use this then performSegue(withIdentifier identifier: String, sender: Any?) is called, right? – Spiky Dec 16 '21 at 05:59
  • performSegue won't be called automatically, you need to explicitly add it where you want to get the new VC. Before performing the segue, Swift will automatically check the prepare for segue function if there is anything you want to do prior to the segue. – nbtown Dec 16 '21 at 06:04
  • I set the identifier to "passResults" but when I try to performSegue and still get the error message like "can't find 'passResults' in scope". – Spiky Dec 16 '21 at 06:24
  • You need to go to your storyboard, click the segue arrow of your desired segue and in the right hand pane, add "passResults" as the identifier for that segue. – nbtown Dec 16 '21 at 06:33
  • I did. That's what I was talking about. But Idk why I still got this error message. – Spiky Dec 16 '21 at 06:39
  • I can't really think of another problem other than that. Double check that you've inserted the ID for the segue correctly: https://stackoverflow.com/questions/39904328/xcode-where-to-assign-the-segue-identifier – nbtown Dec 16 '21 at 06:42
  • Hi. I got this message " this class is not key value coding-compliant for the key grade". when I press the "calculate" button. What problem it could be? Like the other screen did not get the data? – Spiky Dec 17 '21 at 02:33
  • Thank you so much for the help anyway. Thanks! – Spiky Dec 17 '21 at 04:37
0

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

}

}

keshav
  • 96
  • 7
  • if have any problem just tell me i will tell you another method – keshav Dec 16 '21 at 06:00
  • Hi. I am trying to use only one button to both perform the calculation and segue . I wanna pass the calculation results to the UILabel on the other screen connected by navi controller. Can you please check my code on "Calculate" button and prepareSegue, I just feel like I got something wrong here. I provided a image about my app looks like. – Spiky Dec 16 '21 at 06:35
  • you can take var of same type in next screen globally not in viewDidLoad and put these values in that var and in your view did load put them to your lablel – keshav Dec 16 '21 at 08:15
  • i have updated another method in my answer check – keshav Dec 16 '21 at 08:24
  • Hi. I got this message " this class is not key value coding-compliant for the key grade". when I press the "calculate" button. What problem it could be? – Spiky Dec 17 '21 at 02:18