简体   繁体   中英

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. 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. I have tried it in viewDidAppear but I get the same issue.

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

    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. But I get nil on the view controller. I guess I just do not understand how the UIView are loaded. 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.

I hope this is not something super simple but the way swift works is different from my experience with 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)
}

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