목적
- 메뉴가 많아짐에 따라 이동 스타일, 컨트롤러 정보 만으로 다음 페이지로 이동이 가능하도록 하기 위함
구현
enum FlingerStyle {
case navigation
case present
case tabBar
}
class Flinger {
var style: FlingerStyle
var viewController: UIViewController?
init(_ style: FlingerStyle, to viewController: UIViewController?) {
self.style = style
self.viewController = viewController
}
func showViewController(parent: UIViewController) {
switch style {
case .navigation:
if let vc = viewController {
parent.navigationController?.pushViewController(vc, animated: true); return
}
case .present:
if let vc = viewController {
parent.present(vc, animated: true, completion: nil); return
}
(..생략..)
}
}
}
이용 예시
- ViewModel
class SomePageViewModel {
enum Section: Int {
case fruit = 0
case vegetable = 1
( 중간 생략 )
func moveToViewController(row: Int) -> Flinger? {
switch self {
case Section.fruit:
return Fruit(index: row).moveToViewController
default:
return nil
}
}
}
enum Fruit: Int {
case berryInfo = 0
case grapeInfo = 1
(중간 생략)
var moveToViewController: Flinger? {
switch self {
case .berryInfo:
return Flinger(.present, to: UIStoryboard(name: "MyStoryBoard", bundle: nil).instantiateViewController(withIdentifier: "VerryInfoVC"))
}
}
}
}
- ViewModel에서 각 메뉴별로 이동할 컨트롤러 및 스타일을 정의
- ViewController
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let flinger = SomePageViewModel.SectionToLogin(section: indexPath.section).moveToViewController(row: indexPath.row) else { return }
flinger.showViewController(parent: self)
}
}
- Flinger에 정의된 방법으로 ViewController를 보여준다