Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@alexandre-g
Created December 14, 2017 08:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexandre-g/a37b0d55a9a3c5a5c6dc1cd58965dba7 to your computer and use it in GitHub Desktop.
Save alexandre-g/a37b0d55a9a3c5a5c6dc1cd58965dba7 to your computer and use it in GitHub Desktop.
Injecting data to view controller Swift 4
// Inspired by https://github.com/icanzilb/EventBlankApp
extension UIStoryboard {
func instantiateViewController<T: UIViewController>(_ type: T.Type) -> T {
return instantiateViewController(withIdentifier: type.classIdentifier) as! T
}
}
extension UIViewController {
static var classIdentifier: String { return String(describing: self) }
}
class SearchDialogViewController: UIViewController {
var data: [Searchable]!
static func createWith(storyboard: UIStoryboard = R.storyboard.entryFlow(), data: [Searchable]) -> Self {
return storyboard.instantiateViewController(self).then { vc in
vc.data = data
}
}
}
@icanzilb
Copy link

you need to set your data as private so that the only place to change the value is from within the class - e.g. no other code can randomly change it from outside 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment