Clumsy Hamster Clumsy Hamster - 2 months ago 20
iOS Question

Swift prepopulate modal content before opening

I was unable to find an answer like this as of now but in Swift 3 is there a way to pre-populate a modal view controller with data before opening it? I'm thinking not as I believe the modal view controller won't exist at this point.

Here is what I'm trying to achieve. I have a modal that contains a table view that will be bound with the data to be used to filter the content on the parent controller. The parent controller has to pass the data to the modal that is used in it's table view (I do this by having an init in the modal controller). I have a delegate for going back to the parent from the modal but the content in the modal is always the same so I am looking for a way to set the modal table view with the data so I don't have to rebind it every time it is opened like how the following is done.

func presentModal(){
let modalVC = RoadwaysViewController(roadways: roadways)
modalVC.modalPresentationStyle = .popover
modalVC.preferredContentSize = CGSize(width: 300, height: 350)
modalVC.delegate = self
self.present(modalVC, animated: true)

The problem with this is that roadways is always the same, so I would be binding the same data over and over. Is there a way around this?


Give your modally presented view controller a variable that will hold the data,

var data: [MyStruct]? 

and set it when you set it's delegate. Or give it something so it knows what data to pull from your data model. Like pass the earliest date you want data from.