Skip to content

Instantly share code, notes, and snippets.

@sgr-ksmt
Last active October 12, 2023 09:11
Show Gist options
  • Save sgr-ksmt/fc5631dd92701b0464e7b17e3df544fe to your computer and use it in GitHub Desktop.
Save sgr-ksmt/fc5631dd92701b0464e7b17e3df544fe to your computer and use it in GitHub Desktop.
Create UIViewController from storyboard and pass parameter safely.
class UserListViewController: UIViewController {
func presentUserProfile(userId: String) {
let vc = UserProfileViewControllerFactory.create(userId: "xxxxx")
self.present(vc, animated: true, completion: nil)
}
}
public protocol StoryboardInstantiatable {
static var storyboardName: String { get }
static var viewControllerIdentifier: String? { get }
static var bundle: Bundle? { get }
}
public extension StoryboardInstantiatable where Self: UIViewController {
public static var storyboardName: String {
return String(describing: self)
}
public static var viewControllerIdentifier: String? {
return nil
}
public static var bundle: Bundle? {
return nil
}
public static func instantiate() -> Self {
let loadViewController = { () -> UIViewController? in
let storyboard = UIStoryboard(name: storyboardName, bundle: bundle)
if let viewControllerIdentifier = viewControllerIdentifier {
return storyboard.instantiateViewController(withIdentifier: viewControllerIdentifier)
} else {
return storyboard.instantiateInitialViewController()
}
}
guard let viewController = loadViewController() as? Self else {
fatalError()
}
return viewController
}
}
struct UserProfileViewControllerFactory {
private init() {}
static func create(userId: String) -> UserProfileViewController {
let vc = UserProfileViewController.instantiate()
vc.userId = userId
return vc
}
}
class UserProfileViewController: UIViewController {
fileprivate var userId: String!
override func viewDidLoad() {
super.viewDidLoad()
print(userId)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
extension UserProfileViewController: StoryboardInstantiatable {}
@malhal
Copy link

malhal commented Jul 17, 2019

You can now invoke a custom initializer from a creation block that's passed through instantiateInitialViewController(creator:) or instantiateViewController(identifier:creator:). This makes it possible for you to initialize view controllers with additional context and arguments, while taking advantage of defining them in a storyboard through Interface Builder. A custom controller initializer must call its super.init(coder:) method and pass the coder argument that it receives through the creation block. (48313869)

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