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?

  • before assignment, perform 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

2 answers 2

Solved the question by means of delegation. The second I twist the controller, from which it is necessary to transfer the values ​​of the Text Field Outlets to the variables of the first controller twist, extended by the SettingsViewControllerDelegate protocol

 import UIKit protocol SettingsViewControllerDelegate { func fillThePropertiesWith(valueOne: String, valueTwo: String) } class SettingsViewController: UIViewController { var delegate: SettingsViewControllerDelegate? @IBOutlet weak var valueFrom: UITextField! @IBOutlet weak var valueTo: UITextField! @IBAction func sendData(sender: UIButton) { // Присваивание введенных значений переменным let minimumValue = valueFrom.text let maximumValue = valueTo.text delegate?.fillThePropertiesWith(minimumValue!, valueTwo: maximumValue!) } } 

The first twist the controller needs to sign under the protocol and implement the segway method and protocol method

 class ViewController: UIViewController, SettingsViewControllerDelegate { let randomGeneration = RandomGeneration() var min = 0 var max = 100 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "SettingsSegue" { let destanationVC = segue.destinationViewController as! SettingsViewController destanationVC.delegate = self } } func fillThePropertiesWith(valueOne: String, valueTwo: String) { min = Int(valueOne)! max = Int(valueTwo)! } } 

    Most likely, settingsVC.valueFrom was not created at the time of assignment. The controller view is loaded only when necessary (when accessing it, for example, when a window is displayed).

    Before assigning manually, you can call settingsVC.view () or pass the value through a string variable (and display it in the viewWillAppear) or create a textField not in a storyboard, but in the controller's init method (not very good, but sometimes it happens).

    • I tried to bind the SettingsViewController to the same view controller, which describes the randomGeneratePressed method. Accordingly, in the same twist the controller added these outlets. The result is the same - Lex Debash
    • binding a controller is useless - even if it is, it does not yet have a valueFrom. Outlets are good, but they are created only when the controller appears on the screen (before that they are nil). Just refer to settingsVC.view () before assignment. This will load a view with all the outlets. - Valentine
    • Do you mean to open the view with the settings in the simulator and then try to run the randomGeneratePressed method? If so, then I did, the print that I placed in the view view didn’t go off. But it does not help. I changed the data manually in the fields valueFrom and valueTo. Or did I misunderstand you? - Lex Debash
    • Yes, show the view as one of the options. And how exactly does not work? Also falling? Then output the settingsVC to the log. Is it empty or not? If empty, see where you assign it. - Valentine
    • Yes, it is empty. Now I made two TextFields right in the View Controller in which I cheat with random and everything works as it should. I will understand why it does not work in the first version. - Lex Debash