Last active
October 12, 2023 09:11
-
-
Save sgr-ksmt/fc5631dd92701b0464e7b17e3df544fe to your computer and use it in GitHub Desktop.
Create UIViewController from storyboard and pass parameter safely.
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
class UserListViewController: UIViewController { | |
func presentUserProfile(userId: String) { | |
let vc = UserProfileViewControllerFactory.create(userId: "xxxxx") | |
self.present(vc, animated: true, completion: nil) | |
} | |
} |
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
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 | |
} | |
} | |
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
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 {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can now invoke a custom initializer from a creation block that's passed through
instantiateInitialViewController(creator:)
orinstantiateViewController(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 itssuper.init(coder:)
method and pass the coder argument that it receives through the creation block. (48313869)