This is my first question. I'm new to swift and programming in general so please don't laugh if I'm asking stupid question :)
So I hava a SettingsViewControler where users sets their values, lets say they set the temperature value. What i'm trying to do, is to take that temperature value that they input and pass it to my model.swift file, to introduce that value in the formula, to calculate the new value with the input temperature. I hope this make sense.
Is there a way to do that directly by calling the class form VC to the newData class that I created in model.swift file, or I should use some methods like UserDefaults to pass data.
Here is the code example:
First I created a Settings.swift file
// Settings.swift file
import Foundation
class Settings {
var inputTemperature: Float = 0
init(inputTemperature: Float) {
self.inputTemperature = inputTemperature
}
}
Here is the Settings View Controller
//Settings ViewCOntroller. swift file
import UIKit
class SettingsViewController: UIViewController {
@IBOutlet weak var inputTemperatureTextField: UITextField!
var getTemp = Settings(inputTemperature: 72)
override func viewDidLoad() {
super.viewDidLoad()
}
func getValues() {
getTemp.inputTemperature = (inputTemperatureTextField.text! as NSString).floatValue
}
}else if textField == inputTemperatureTextField {
textField.resignFirstResponder()
getValues()
}
So now I have another Calculations.swift file where I want to get the inputTemperature value to use it in the formula
//Calculation. swift file
import Foundation
class Calculations {
var inputA: Float = 0
var inputB: Float = 0
var resultC: Float = 0
init(inputA: Float, inputB: Float) {
self.inputA = inputA
self.inputB = inputB
}
// Here i want to add the temperature value
func calc() {
resutC = inputA * inputC // * inputTemperature
}
I want the get (inputTemperatureTextField.text! as NSString).floatValue value from SettingsView COntroller to introduce it in the formula located Calculation.swift file
Thanks
You should really post some of your code to give us some insight in what exactly it is you want. Lets say in your model you have a temperature value like
var temp: Int?
Then you can initialize it in your VC, and access the temp value
var model = Model()
let model.temp = inputTextField.text
If you are using model.swift
for calculation. You will create an object of the model-class in the View controller, after creating an object you can pass the test field value to the model.
var modelObject: model? = model();
modelObject.temprature = txtTemprature.text;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.