简体   繁体   中英

How to pass data between a UIViewController and an NSObject in Swift

I have a viewController and an NSObject file (called autoPDFBuilder). I want to pass the data, retrieved and stored in variables in the viewController (called infoViewController), to variables in autoPDFBuilder. If I try using a segue to send the information, then it doesn't work because autoPDFBuilder is not a UIViewController. Obviously, if I send the data to any other UIViewController, using a segue, then it works, but I need to get the data to the NSObject file so that I can create a PDF file with the data. Here's the segue code:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

let client = clientTextField.text!
let claimNumber = claimNumberTextField.text!
let fileNumber = fileNumberTextField.text!
let lossDate = lossDateTextField.text!
let insured = insuredTextField.text!
let claimant = claimantTextField.text!
let picsBy = picsByTextField.text!
let dateTaken = dateTakenTextField.text!


let vc = segue.destination as! autoPDFBuilder
vc.client = client
vc.claimNumber = claimNumber
vc.fileNumber = fileNumber
vc.dateOfLoss = lossDate
vc.insured = insured
vc.claimant = claimant
vc.picsBy = picsBy
vc.dateTaken = dateTaken

}

And here's the error that I get if I try to use the segue... because autoPDFBuilder is an NSObject and not a UIViewController: " Cast from 'UIViewController' to unrelated type 'autoPDFBuilder' always fails. "

I also tried getting the data by first, sending it to the claimTypeViewController via the segue in infoViewController (this works correctly... the data is passed). Then, in autoPDFBuilder, access the data that is now located in claimTypeViewController like such:

class autoPDFBuilder: NSObject {

var client = String()
var claimNumber = String()
var fileNumber = String()
var dateOfLoss = String()
var insured = String()
var claimant = String()
var picsBy = String()
var dateTaken = String()

let vc = claimTypeViewController()
client = vc.client

}

This does not work either. As I mentioned before, the data is successfully passed to claimTypeViewController from infoViewController (since they're both UIViewControllers), but when I try to access the data from autoPDFCreator, the variables that are holding the data in claimTypeViewControllers are completely empty. There's nothing in them. I ran print tests after every step, so I know where the issue is... it's simply something with data not sharing between an NSObject file and a UIViewController file.

I also tried sending the data in the same style above (in the second block of code) using a third-party.swift file, but that didn't work either. In the third-party file, called Variables, I created a constant

let infoController = infoViewController()

and then created a variable to store the client variable from infoViewController

var theClient = String()

then place retrieve the data into the variable, like such:

theClient = infoController.client

Then do the same thing in autoPDFCreator to retrieve the data from the Variables file. None of this works of course. I've tried many other methods as well - no luck.

I've done lots of research and can't seem to figure it out. There was only one similar thread, but the answer to the problem was not made clear in the user's response.

Please help me out and make your answers complete and illustrated... I learn by seeing it visually. I'm a beginner, so also please explain your answers.

Thank you for your kindness and help!

Assuming you currently have your segue connected to a button tap...

  • Delete that segue
  • connect an @IBAction to your button tap
  • in that action, instantiate a AutoPDFBuilder object (side note: use LeadingUpperCase for class names, leadingLowerCase for variables)

Something along these lines:

@IBAction func buttonTapped(_ sender: UIButton) {
    
    let client = clientTextField.text!
    let claimNumber = claimNumberTextField.text!
    let fileNumber = fileNumberTextField.text!
    let lossDate = lossDateTextField.text!
    let insured = insuredTextField.text!
    let claimant = claimantTextField.text!
    let picsBy = picsByTextField.text!
    let dateTaken = dateTakenTextField.text!

    // instantiate a AutoPDFBuilder object
    let pdfBuilder = AutoPDFBuilder()
    pdfBuilder.client = client
    pdfBuilder.claimNumber = claimNumber
    pdfBuilder.fileNumber = fileNumber
    pdfBuilder.dateOfLoss = lossDate
    pdfBuilder.insured = insured
    pdfBuilder.claimant = claimant
    pdfBuilder.picsBy = picsBy
    pdfBuilder.dateTaken = dateTaken

    // call a function in the AutoPDFBuilder class
    pdfBuilder.createPDF()
    
}

class AutoPDFBuilder: NSObject {
    
    var client = String()
    var claimNumber = String()
    var fileNumber = String()
    var dateOfLoss = String()
    var insured = String()
    var claimant = String()
    var picsBy = String()
    var dateTaken = String()

    func createPDF() -> Void {
        // do what you do to bild the pdf
    }
    
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM