swift。如何将计算结果传递到另一个屏幕?

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

我是swift初学者。在这个项目中,我需要将用户的输入存储到字典中。然后我用数据做一些计算。我有一个“计算”按钮,按下时,程序开始计算结果并将结果传递到另一个屏幕。我被困在这里只是无法传递数据。也许我的“计算”代码有问题。请帮忙


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()
        
   
      
    }

你从不打电话给 performSegue(withIdentifier identifier: String, sender: Any?)。我假设你想在计算完成后执行 segue,对吗?那是你应该调用 performSegue 的时候。 不要忘记在故事板中为您的 segue 提供一个 ID,并将此 ID 作为 identifier 传递给 performSegue 函数。

有很多方法可以做到这一点,但您可以使用 UserDefaults

//为了保存你可以使用的值 UserDefaults.standard.setValue(有吗?,forKey:字符串)

//并得到 让 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)
}

}

//你的第二个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

}

}

您的 UIViewController 中存在一些错误

在你的

@IBAction func add(_ sender: UIButton) 

而不是

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

你应该去

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

并将值传递给 ResultsViewController

  • 从故事板添加 segue 并将标识符命名为“SomeID”

  • 然后到UIViewcontrollerclass并添加

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

你的 calc 函数。

完整代码 = https://github.com/alirzaeram/Pass-the-calculation-result