Created
December 14, 2017 08:43
-
-
Save alexandre-g/a37b0d55a9a3c5a5c6dc1cd58965dba7 to your computer and use it in GitHub Desktop.
Injecting data to view controller Swift 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 👍