Skip to content

Instantly share code, notes, and snippets.

@aryzae
Last active March 6, 2018 03:24
Show Gist options
  • Save aryzae/04353d7e0a6614488ab5fc04828a9eed to your computer and use it in GitHub Desktop.
Save aryzae/04353d7e0a6614488ab5fc04828a9eed to your computer and use it in GitHub Desktop.
画面遷移のProtocol(たぶんあまりよくない例)
import UIKit
enum PresentDestination {
case viewControllerA
case viewControllerB(content: Content)
var viewController: UIViewController {
switch self {
case .viewControllerA:
return ViewControllerA.initiateWithNavigation()
case .viewControllerB(let content):
return ViewControllerB.initiateWithNavigation(content: content)
}
}
}
enum PushDestination {
case viewControllerC
case viewControllerD(identifier: String)
var viewController: UIViewController {
switch self {
case .viewControllerC:
return ViewControllerC.initiate()
case .viewControllerD(let identifier):
return ViewControllerD.initiate(identifier: identifier)
}
}
}
protocol Routable {
func present(_ destination: PresentDestination, from viewController: UIViewController, animated: Bool, completion: (() -> Void)?)
func navigationControllerPush(_ destination: PushDestination, from viewController: UIViewController, animated: Bool)
}
extension Routable {
func present(_ destination: PresentDestination, from viewController: UIViewController, animated: Bool, completion: (() -> Void)? = nil) {
viewController.present(destination.viewController, animated: animated, completion: completion)
}
func navigationControllerPush(_ destination: PushDestination, from viewController: UIViewController, animated: Bool) {
viewController.navigationController?.pushViewController(destination.viewController, animated: true)
}
}
////
使用時
////
1. 画面遷移処理を書きたいViewControllerやPresenterにRoutableプロトコル適用
2. 画面遷移したい箇所で、
// ViewControllerの時
func showViewControllerA() {
present(.viewControllerA, from: self, animated: true)
}
func pushViewControllerD() {
navigationControllerPush(.viewControllerD(identifier: "fugaId"), from: self, animated: true)
}
// Presenterの時
func showViewControllerA() {
guard let vc = view as? HogeViewController else {
return
}
present(.viewControllerA, from: vc, animated: true)
}
func pushViewControllerD() {
guard let vc = view as? HogeViewController else {
return
}
navigationControllerPush(.viewControllerD(identifier: "fugaId"), from: vc, animated: true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment