简体   繁体   English

传递的数据模型输出为零,即使在调试中我在传递之前设置了 var

[英]A passed data model is coming out as nil even though in debug I was the var get set before being passed

I am trying to pass a data model from my initial view controller to the view controller that is now on screen.我试图将数据模型从我的初始视图控制器传递到现在在屏幕上的视图控制器。 I have a container view that shows a pdf.我有一个显示 pdf 的容器视图。 When I run the code the document that is passed into the container is nil for some reason.当我运行代码时,由于某种原因,传递到容器中的文档为零。

I have used the debugger and watch it get set in the initial view controller, but when the next storyboard is loaded that var is now nil for some reason.我已经使用了调试器并观察它在初始视图控制器中设置,但是当加载下一个故事板时,由于某种原因,var 现在为零。 I have tried it in viewDidAppear but I get the same issue.我已经在viewDidAppear尝试过,但我遇到了同样的问题。

My initial view controller (the homepage)我的初始视图控制器(主页)

let documentGet = Data.documentModel[selectedRow - 1]
            let storyboard = UIStoryboard(name: String(describing: NoteTakingViewController.self), bundle: nil)
            let vc = storyboard.instantiateInitialViewController() as! NoteTakingViewController
            vc.documentSet = documentGet

            //self.navigationController?.pushViewController(vc, animated: true)
            //self.present(vc, animated: true, completion: nil)
            self.show(vc, sender: selectedRow)

The next view controller for the storyboard where I pass the previous vc.documentSet = documentGet我传递上一个vc.documentSet = documentGet的故事板的下一个视图控制器

    let pdfViewer = PDFView()
    @IBOutlet weak var PDFClass: PDFViewClass!
    var documentSet:DocumentModel!


    override func viewDidLoad() {
        super.viewDidLoad()
        PDFClass.setDocument(document: self.documentSet) <----(this is where the error occurs)
        self.title = documentSet.title
        navigationController?.navigationBar.prefersLargeTitles = false
    }

This is the view controller for the container view.这是容器视图的视图控制器。 It is a custom class since I have tried to get it to work on the previous controller but it will block the tool bar that I am trying to figure out underneath the navigation bar (yes directly under the navigation bar)这是一个自定义类,因为我试图让它在以前的控制器上工作,但它会阻止我试图在导航栏下方找出的工具栏(是的,直接在导航栏下方)

    private func configurePDF() {
        pdfViewer.translatesAutoresizingMaskIntoConstraints = true
        view.addSubview(pdfViewer)
    }

    func setDocument(document: DocumentModel!) {
        configurePDF()
        let doc = PDFDocument(url: document.url)
        pdfViewer.document = doc
    }

I expect the pdf data to go from home page -> view controller -> container view controller.我希望 pdf 数据从主页 -> 视图控制器 -> 容器视图控制器。 But I get nil on the view controller.但是我在视图控制器上得到了零。 I guess I just do not understand how the UIView are loaded.我想我只是不明白 UIView 是如何加载的。 My thought process is the that the var in the view controller is set with vc.documentSet = documentGet then when that view is up it will pass the var to the container view.我的想法是,视图控制器中的 var 是用vc.documentSet = documentGet设置的,然后当该视图启动时,它会将 var 传递给容器视图。

I hope this is not something super simple but the way swift works is different from my experience with java.我希望这不是超级简单的事情,但是 swift 的工作方式与我使用 java 的经验不同。

You can solve this with some defensive programming and a property observer:您可以通过一些防御性编程和属性观察器来解决这个问题:

let pdfViewer = PDFView()
@IBOutlet weak var PDFClass: PDFViewClass!
var documentSet: DocumentModel? {
  didSet {
        self.title = documentSet?.title
        if documentSet != oldValue {
            setDocument(to: documentSet)
        }
     }
 }

override func viewDidLoad() {
    super.viewDidLoad()
    setDocument(to: documentSet)
    navigationController?.navigationBar.prefersLargeTitles = false
}

private func setDocument(to document: DocumentModel?) {
    guard let validDocument = document else { return }
    PDFClass?.setDocument(document: validDocument)
}

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

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