I can't find ANYWHERE a simple, up-to-date tutorial of how to simply share (pass) variable data between multiple view controllers using Dependency Injection.
I followed this tutorial... The Right Way to Share State Between Swift View Controllers
...But it's out-of-date (Swift 3.0) so it doesn't work and I get this error: Unexpectedly found nil while implicitly unwrapping an Optional value
Can someone please just quickly take a look at that short tutorial and let me know how to fix the issue? The post author isn't replying to the questions in his comments. Kindly don't reply unless you've looked at the tutorial and can correct whatever is wrong in it.
I'm a new programmer, trying to learn how to do things the right way, instead of cheating, but I can't seem to really understand the more complex tutorials out there. The one linked above is the only one I understood... but of course it has the error lol.
Please show your answer in code. Thank you!
Here's an example of my code... it's very similar to his, except I'm trying to gather data in the "InfoViewController" via text fields, pass them to the "ModelController," and then access them in an NSObject .class called "AutoPDFBuilder." The initial (root) view controller is called ViewController and I inject into it via the AppDelegate, as in the tutorial.
App Delegate:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
if let homeViewController = window?.rootViewController as? ViewController {
homeViewController.modelController = ModelController()
}
return true
}
}
ViewController:
class ViewController: UIViewController {
var modelController: ModelController!
@IBAction func newReportButton(_ sender: Any) {
performSegue(withIdentifier: "homeSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let infoViewController = segue.destination as? InfoViewController {
infoViewController.variableController = modelController
}
}
}
InfoViewController (gather the information in textFields here):
struct pdfVariables {
let theClient: String
let theClaimNumber: String
let theFileNumber: String
let theLossDate: String
let theInsured: String
let theClaimant: String
let thePicsAreBy: String
let theDateTaken: String
}
class InfoViewController: UIViewController, UITextFieldDelegate {
//TEXT FIELDS
@IBOutlet weak var clientTextField: UITextField!
@IBOutlet weak var claimNumberTextField: UITextField!
@IBOutlet weak var fileNumberTextField: UITextField!
@IBOutlet weak var lossDateTextField: UITextField!
@IBOutlet weak var insuredTextField: UITextField!
@IBOutlet weak var claimantTextField: UITextField!
@IBOutlet weak var picsByTextField: UITextField!
@IBOutlet weak var dateTakenTextField: UITextField!
//DECLARATIONS
var variableController: ModelController!
@IBAction func infoDoneButton(_ sender: Any) {
var client = String()
var claimNumber = String()
var fileNumber = String()
var lossDate = String()
var insured = String()
var claimant = String()
var picsBy = String()
var dateTaken = String()
if clientTextField.text != nil {
client = clientTextField.text!
} else {
client = "Not applicable"
}
if claimNumberTextField != nil {
claimNumber = claimNumberTextField.text!
} else {
claimNumber = "Not applicable"
}
if fileNumberTextField != nil {
fileNumber = fileNumberTextField.text!
} else {
fileNumber = "Not applicable"
}
if lossDateTextField != nil {
lossDate = lossDateTextField.text!
} else {
lossDate = "Not applicable"
}
if insuredTextField != nil {
insured = insuredTextField.text!
} else {
insured = "Not applicable"
}
if claimantTextField != nil {
claimant = claimantTextField.text!
} else {
claimant = "Not applicable"
}
if picsByTextField != nil {
picsBy = picsByTextField.text!
} else {
picsBy = "Not applicable"
}
if dateTakenTextField != nil {
dateTaken = dateTakenTextField.text!
} else {
dateTaken = "Not applicable"
}
let claimInfoData = pdfVariables(theClient: client, theClaimNumber: claimNumber, theFileNumber: fileNumber, theLossDate: lossDate, theInsured: insured, theClaimant: claimant, thePicsAreBy: picsBy, theDateTaken: dateTaken)
func load() {
variableController.thePDFInfo = claimInfoData
}
performSegue(withIdentifier: "infoSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("The info segue was executed.")
let autoPDFBuilder = AutoPDFBuilder()
autoPDFBuilder.variableController = variableController
}
}
AutoPDFBuilder:
class AutoPDFBuilder: NSObject {
var variableController: ModelController!
var client = String()
func createPDF() -> Data {
client = variableController.thePDFInfo.theClient
print("The client is \(client)")
}
}
ModelController:
class ModelController {
var thePDFInfo = pdfVariables(theClient: "N/A", theClaimNumber: "N/A", theFileNumber: "N/A", theLossDate: "N/A", theInsured: "N/A", theClaimant: "N/A", thePicsAreBy: "N/A", theDateTaken: "N/A")
}
The error is coming in the InfoViewController where I try to assign "claimInfoData" to "variableController.thePDFInfo". This is the area, as stated before: Unexpectedly found nil while implicitly unwrapping an Optional value.