简体   繁体   English

如何在 Swift 中的 UIViewController 和 NSObject 之间传递数据

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

I have a viewController and an NSObject file (called autoPDFBuilder).我有一个 viewController 和一个 NSObject 文件(称为 autoPDFBuilder)。 I want to pass the data, retrieved and stored in variables in the viewController (called infoViewController), to variables in autoPDFBuilder.我想将在 viewController(称为 infoViewController)中的变量中检索和存储的数据传递给 autoPDFBuilder 中的变量。 If I try using a segue to send the information, then it doesn't work because autoPDFBuilder is not a UIViewController.如果我尝试使用 segue 发送信息,那么它不起作用,因为 autoPDFBuilder 不是 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.显然,如果我使用 segue 将数据发送到任何其他 UIViewController,那么它可以工作,但我需要将数据获取到 NSObject 文件,以便我可以使用数据创建 PDF 文件。 Here's the segue code:这是segue代码:

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. "这是我尝试使用 segue 时遇到的错误......因为 autoPDFBuilder 是 NSObject 而不是 UIViewController:“从 'UIViewController' 转换为不相关类型 'autoPDFBuilder' 总是失败。

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).我还尝试首先获取数据,通过 infoViewController 中的 segue 将其发送到 claimTypeViewController(这工作正常......数据已传递)。 Then, in autoPDFBuilder, access the data that is now located in claimTypeViewController like such:然后,在 autoPDFBuilder 中,访问现在位于 claimTypeViewController 中的数据,如下所示:

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.正如我之前提到的,数据已成功从 infoViewController 传递给 claimTypeViewController(因为它们都是 UIViewControllers),但是当我尝试从 autoPDFCreator 访问数据时,在 claimTypeViewControllers 中保存数据的变量完全为空。 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.我在每一步之后都运行了打印测试,所以我知道问题出在哪里......这只是数据在 NSObject 文件和 UIViewController 文件之间不共享的问题。

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.我还尝试使用第三方.swift 文件以上述相同样式(在第二个代码块中)发送数据,但这也不起作用。 In the third-party file, called Variables, I created a constant在名为 Variables 的第三方文件中,我创建了一个常量

let infoController = infoViewController()

and then created a variable to store the client variable from infoViewController然后创建一个变量来存储来自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.然后在 autoPDFCreator 中执行相同的操作以从变量文件中检索数据。 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...假设您当前已将 segue 连接到按钮水龙头...

  • Delete that segue删除那个segue
  • connect an @IBAction to your button tap@IBAction连接到您的按钮点击
  • in that action, instantiate a AutoPDFBuilder object (side note: use LeadingUpperCase for class names, leadingLowerCase for variables)在该操作中,实例化AutoPDFBuilder object(旁注:class 名称使用LeadingUpperCase,变量使用leadingLowerCase)

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
    }
    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当单击 Swift 中 NSObject 内部的 tableView 中的项目时,如何在 NSObject Class 内部的 UIViews 或 UIViewController 之间正确切换? - How to switch properly between UIViews or UIViewController inside NSObject Class when clicked the item in tableView inside of NSObject in Swift? 如何在Swift中将数据从UITableViewRowAction传递到UIViewController? - How to pass data to a UIViewController from a UITableViewRowAction in Swift? 在nsobject和viewcontroller之间传递数据的最佳方法 - Best way to pass data between nsobject and a viewcontroller 如何将UIViewController作为参数传递给swift 3? - How to pass UIViewController as a parameter swift 3? 将数据从NSObject发送到UIViewController - Sending data from NSObject to UIViewController NSObject不提供数据UIViewController - NSObject does not give data UIViewController Swift-如何将数据从UITableViewCell类传递到UIViewController - Swift - How to Pass Data From UITableViewCell Class to UIViewController 如何将数据传递到 UIView 一侧的嵌入式 UIViewController - Swift 5 - How to pass data to a embedded UIViewController in side a UIView - Swift 5 在Swift中的NSObject类中执行UIViewController中的函数 - execute function in UIViewController from NSObject Class in swift 将NSString从NSObject中的didUpdateLocations传递到UIViewController - Pass NSString from didUpdateLocations in NSObject to a UIViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM