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
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.there is a lot of method to do this you can use UserDefaults
0r//
}
}
You have some bugs in your UIViewController
in your
instead of
you should go for
and for passing values to
ResultsViewController
Add segue from the storyboard and name the Identifier "SomeID"
Then go to the
UIViewcontroller
class and addto your
calc
func.Full Code = https://github.com/alirzaeram/Pass-the-calculation-result