Display Name
Display Name

Reputation: 1035

Xcode Moving from Storyboard

I have a Xcode project using storyboards.

I have the typical setup,

Now the project is growing making using storyboards impractical. We are taking a code approach loading views from code. The issue we are facing is how to pass data to new view before 'viewdidload' is called or what's the correct process to use.

We are loading view using

    let nib = UINib(nibName: "TestView", bundle: nil)
    let view = nib.instantiate(withOwner: self, options: nil).first as! TestViewController
    view.delegate = self
    view.dataModel = TestViewDataModel()
    present(view, animated: true, completion: nil)  

Issue we have is 'instantiate' calls viewdidload but ideally it needs data from 'datamodel'

Thanks

Upvotes: 1

Views: 56

Answers (1)

Alexander
Alexander

Reputation: 63157

Add a new initializer to TestViewController that takes accepts whatever data you need as parameters.

From within it, call super.init(nibName: "TestView", bundle: nil). UIViewController's initializer will take care of finding the nib, instantiating itself from it, and settings itself as the owner.

class TestViewController: UIViewController {

    let dataModel: TestViewDataModel

    init(dataModel: TestViewDataModel) {
        super.init(nibName: "TestView", bundle: nil)
        self.dataModel = dataModel
    }

    //...
}

Upvotes: 1

Related Questions