Skip to content

Instantly share code, notes, and snippets.

@hmhmsh
Created July 13, 2018 05:07
Show Gist options
  • Save hmhmsh/633bc6000c6d2eaf79d7521e9b3abdf5 to your computer and use it in GitHub Desktop.
Save hmhmsh/633bc6000c6d2eaf79d7521e9b3abdf5 to your computer and use it in GitHub Desktop.
Navigator
struct Navigator {
static func toMainViewController(from parent: UIViewController) {
self.present(from: parent, to: StoryboardBuilder.mainViewController)
}
// 遷移先のViewControllerに値を渡す場合
static func toDetailViewController(from parent: UIViewController, id: String) {
let detail = StoryboardBuilder.detailViewController
detail.id = id
self.present(from: parent, to: detail)
}
private static func present(from parent: UIViewController, to content: UIViewController) {
parent.present(content, animated: true, completion: nil)
}
}
struct StoryboardBuilder {
private static func storyboard(name: StoryboardName) -> UIStoryboard {
return UIStoryboard(name: name.rawValue, bundle: Bundle.main)
}
private static func viewController<T: UIViewController>(name: StoryboardName, id: StoryboardID) -> T {
guard let vc = storyboard(name: name).instantiateViewController(withIdentifier: id.rawValue) as? T else {
fatalError("Error: StoryboardBuilder: No cast ViewControllerType from storyboard to ReturnValueType")
}
return vc
}
static var mainViewController: MainViewController {
return viewController(name: .Main, id: .MainViewController)
}
static var detailViewController: DetailViewController {
return viewController(name: .Detail, id: .DetailViewController)
}
}
enum StoryboardID: String {
case MainViewController
case DetailViewController
}
// XXX.storyboardのXXXの部分
enum StoryboardName: String {
case Main
case Detail
}
func onClickButtonToMain() {
Navigator.toMainViewController(from: self)
}
func onClickButtonToDetail() {
Navigator.toDetailViewController(from: self)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment