Created
July 13, 2018 05:07
-
-
Save hmhmsh/633bc6000c6d2eaf79d7521e9b3abdf5 to your computer and use it in GitHub Desktop.
Navigator
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 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) | |
} | |
} |
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
enum StoryboardID: String { | |
case MainViewController | |
case DetailViewController | |
} |
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
// XXX.storyboardのXXXの部分 | |
enum StoryboardName: String { | |
case Main | |
case Detail | |
} |
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
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