在Swift4

时间:2018-04-19 10:39:19

标签: swift4 xcode9.3

我是新来的,想问几天一直在为我工作的问题。我正在学习Swift 4,而且我已经走了很长一段路。我真的不知道该怎么做了,而且我的swift书也没有帮助我。

我已经创建了一个小型测试应用程序,其中只需要收费。

有5个视图控制器。第一个有4个按钮可以到达其中一个4并在文本框中输入一个数字。然后在标签中的第一个视图控制器中输出该数字。显示数字,甚至在重新启动应用程序后再次显示最后输入的数字。

但现在我想在第一个viewcontroller中收取数字。我该如何修复代码?

我的视口:

my viewports

来自主视口的

代码:

import UIKit

class ViewController: UIViewController, sendValue1, sendValue2, sendValue3, sendValue4 {



    @IBOutlet weak var value1: UILabel!
    @IBOutlet weak var value2: UILabel!
    @IBOutlet weak var value3: UILabel!
    @IBOutlet weak var value4: UILabel!

    @IBOutlet weak var calculatedValue1: UILabel! // here i want to see the calculated value like from the label 1-4...value1 + value2 + value3 + value4 = ???
    @IBOutlet weak var calculatedValue2: UILabel! // here the same like in claculatedValue1 value but with "-" or "*" or something else...




    func value1Data(data: String) {
        value1.text = data
        UserDefaults.standard.set(value1.text, forKey: "value1")
    }
    func value2Data(data: String) {
        value2.text = data
        UserDefaults.standard.set(value2.text, forKey: "value2")
    }
    func value3Data(data: String) {
        value3.text = data
        UserDefaults.standard.set(value3.text, forKey: "value3")
    }
    func value4Data(data: String) {
        value4.text = data
        UserDefaults.standard.set(value4.text, forKey: "value4")
    }


    override func viewDidAppear(_ animated: Bool) {
        if let lastValue1Data = UserDefaults.standard.object(forKey: "value1") as? String {
            value1.text = lastValue1Data
        }
        if let lastValue2Data = UserDefaults.standard.object(forKey: "value2") as? String {
            value2.text = lastValue2Data
        }
        if let lastValue3Data = UserDefaults.standard.object(forKey: "value3") as? String {
            value3.text = lastValue3Data
        }
        if let LastValue4Data = UserDefaults.standard.object(forKey: "value4") as? String {
            value4.text = LastValue4Data
        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "VC1" {
            let SendingVC1:  Value1ViewController = segue.destination as! Value1ViewController
            SendingVC1.delegate = self
        }
        if segue.identifier == "VC2" {
            let SendingVC2: Value2ViewController = segue.destination as! Value2ViewController
            SendingVC2.delegate = self
        }
        if segue.identifier == "VC3" {
            let SendingVC3: Value3ViewController = segue.destination as! Value3ViewController
            SendingVC3.delegate = self
        }
        if segue.identifier == "VC4" {
            let SendingVC4: Value4ViewController = segue.destination as! Value4ViewController
            SendingVC4.delegate = self
        }
    }

    @IBAction func unwindToView1(_ segue: UIStoryboardSegue) {

    }

以及其他四个中的一个代码:

import UIKit

protocol sendValue1 {
    func value1Data(data: String)
}

class Value1ViewController: UIViewController {
    var delegate: sendValue1? = nil
    @IBOutlet weak var textValue1: UITextField!

    @IBAction func done(_ sender: Any) {
        if delegate != nil {
            if textValue1.text != nil {
                let data = textValue1.text
                delegate?.value1Data(data: data!)
                dismiss(animated: true, completion: nil)
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

为什么结果总是零?

let a = Float(value3.text!) ?? 0
let b = Float(value4.text!) ?? 0
let SUM = a + b
calculatedValue1.text = "\(SUM)" + "m"

无论我做什么,都不会处理这些数字......