I know that SO has a lot of questions of this kind, but there all the answers boil down to what you need to do to retrieve the option via if let value = opcionalValue { newValue = value }
and the like. My problem is not the extraction of the optional, as such, but the fact that TextField outlets are not initialized by the values entered into the appropriate fields and do not accept new values.
There is a View Controller in which there are two Text Field properties. These default properties already have the values that I set in the storyboard. Those. these values should no longer be nil
import UIKit class SettingsViewController: UIViewController { @IBOutlet weak var valueFrom: UITextField! @IBOutlet weak var valueTo: UITextField!
I need to transfer the values of these fields to another View Controller
import UIKit class ViewController: UIViewController { let settingsVC = SettingsViewController() var min = 0 var max = 100 @IBOutlet weak var randomNumber: UILabel! @IBOutlet weak var yesOrNo: UILabel! @IBAction func randomGeneratePressed(sender: AnyObject) { if let abc = settingsVC.valueFrom.text { print(abc) } else { settingsVC.valueFrom.text = "abc" let abc = settingsVC.valueFrom?.text print(abc) }
The code in the randomGeneratePressed
function is not represented as original, but as an example. Here on the line if let abc = settingsVC.valueFrom.text
application crashes with the error fatal error: unexpectedly found nil while unwrapping an Optional value
. If this line is written like this: if let abc = settingsVC.valueFrom.?text
with a question mark, then there will be no error, but I will get nil on output. Those. it turns out that the properties from the first View Controller are not initialized and even assigning a value does not help. Why is that?
let vi = settingsVC.view
to force the application to load the view. but generally not the best architecture to use a storyboard for model storage - Max Mikheyenko