ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

 

Coordinator ํŒจํ„ด์„ ๋งŒ๋“  ์‚ฌ๋žŒ์ด ์ž‘์„ฑํ•œ ์ œ์ž‘ ์ด์œ ์™€ ๊ฐœ๋… ์†Œ๊ฐœ ์ž…๋‹ˆ๋‹ค.

https://khanlou.com/2015/01/the-coordinator/

์“ฐ์ž„์ด๋‚˜ ์ฝ”๋“œ๋ฅผ ๋ณด๊ธฐ์— ์•ž์„œ

์–ด๋–ค ๊ฐœ๋…(์–ด๋–ค ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ• ๋ ค๊ณ )์œผ๋กœ ๋งŒ๋“ค์—ˆ๋Š”์ง€ ์„ค๋ช…๋˜์–ด ์žˆ๋„ค์š”

 

 

 

 

Coordinator ํŒจํ„ด์ด๋ž€ 

 

์—ฌ๋Ÿฌ VC๊ฐ„ Navigation์˜ ์ด๋™์ด๋‚˜ ๋ฐ์ดํ„ฐ์ด๋ฒคํŠธ์˜ ์ „๋‹ฌ์„ ๊น”๋”ํ•˜๊ณ  ๋ถ„๋ฆฌ๋œ ๋ฐฉ์‹์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

๊ฐ VC๋Š” Coordinator ์™€ ์—ฐ๊ฒฐ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

 

 

๊ทธ๋ž˜์„œ Coordinator๋ฅผ 

VC๊ฐ„ ์ค‘์žฌ์ž ์—ญํ• ์„ ํ•˜๊ณ  present์™€ dismiss๋ฅผ ์ œ์–ดํ•˜๋Š” ์—ญํ•  (ํ”„๋กœํ† ์ฝœ)๋กœ ๋ด๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค

 

 

 

 

๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. (์œ„ 3๊ฐ€์ง€๋ฅผ ์ ์šฉ)

 

Home VC์—์„œ ํƒญํ–ˆ์„๋•Œ Detail VC๋กœ ๋„˜์–ด๊ฐ€๋Š” ํ™”๋ฉด์ „ํ™˜์„ Coordinator๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

protocol Coordinator {
    func start()
}

 

1) ๋จผ์ € base coordinator ํ”„๋กœํ† ์ฝœ์„ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.

 

 

 

 

2) Home coordiantor ์„ ๊ตฌํ˜„ํ•˜๊ณ , base coordinator๋ฅผ ์ฑ„ํƒํ•ฉ๋‹ˆ๋‹ค.

 

class HomeCoordinator: Coordinator {
    var navigationController: UINavigationController

    init(navigationController: UINavigationController) {
        self.navigationController = navigationController
    }

    func start() {
        let homeVC = HomeViewController(coordinator: self)
        homeVC.coordinator = self
        navigationController.pushViewController(homeVC, animated: true)
    }

    func showDetail(for item: String) {
        let detailCoordinator = DetailCoordinator(navigationController: navigationController, item: item)
        detailCoordinator.start()
    }
}

 

 

HomeCoordinator์—๋Š” 

 

Navigator๋ฅผ ๋ฐ›์•„์„œ ๊ทธ Navigator๋กœ HomeVC๋ฅผ ๋ณด์—ฌ์ฃผ๊ฑฐ๋‚˜,

DeatilVC๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

(HomeVC์™€ ๋™์ผํ•˜๊ฒŒ Detail Coordinator์—์„œ DetailVC๊ฐ€ ๋ณด์—ฌ์ง€๋„๋ก ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.)

 

 

 

 

Home coordiantor์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ base coordinator๋ฅผ ์ฑ„ํƒํ•œ

Detail Coordinator ์ž…๋‹ˆ๋‹ค.

class DetailCoordinator: Coordinator {
    var navigationController: UINavigationController
    var item: String

    init(navigationController: UINavigationController, item: String) {
        self.navigationController = navigationController
        self.item = item
    }

    func start() {
        let detailVC = DetailViewController(item: item)
        detailVC.coordinator = self
        navigationController.pushViewController(detailVC, animated: true)
    }
}

 

 

 

 

3) ๋งˆ์ง€๋ง‰์œผ๋กœ HomeVC๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.

 

HomeCoordinator๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ ,

ํƒญํ–ˆ์„๋•Œ Home coordinator์—์„œ Detail VC๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์ด๋ฒคํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.

 

class HomeViewController: UIViewController {
    weak var coordinator: HomeCoordinator?

    // ... your other code ...

    func didSelectItem(_ item: String) {
        coordinator?.showDetail(for: item)
    }
}


// Detail VC
class DetailViewController: UIViewController {
    weak var coordinator: DetailCoordinator?
    var item: String

    init(item: String) {
        self.item = item
        super.init(nibName: nil, bundle: nil)
    }

    // ... your other code ...
}

 

 

 

๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด Coordinator ํŒจํ„ด์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

 

์ด๋ ‡๊ฒŒ ์“ฐ๋Š” ์ด์œ ์™€ ์žฅ์ ์œผ๋กœ๋Š”

 

1. ๊ด€์‹ฌ์‚ฌ ๋ถ„๋ฆฌ

2. Navigation์˜ ํ๋ฆ„ ๊ด€์ ์—์„œ VC๊ฐ„ ์˜์กด์„ฑ์„ ๋‚ฎ์ถœ ์ˆ˜ ์žˆ๋‹ค. (Detail VC๊ฐ„์ ‘ ํ˜ธ์ถœ)

3. ๊ทธ๋ฆฌ๊ณ  ๋กœ๊ทธ์•„์›ƒ, ๋กœ๊ทธ์ธ ๋“ฑ ์ด๋Ÿฐ ์ƒํƒœ๊ฐ’๋“ค์„ Coordinator์—์„œ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์–ด์„œ ๊น”๋”ํ•˜๋‹ค.

 

๋“ฑ์ด ์žˆ๋Š”๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

 

 

 

 

 

๋ถ€์กฑํ•œ ๊ธ€ ์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

ํ‹€๋ฆฐ ๋ถ€๋ถ„์ด๋‚˜ ๊ฐœ์„ ์‚ฌํ•ญ ์•Œ๋ ค์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค:)

 

 

 

 

 

 

 

 

์ถ”๊ฐ€๋กœ)

 

zeddios๋‹˜์ด ์ž‘์„ฑํ•˜์‹ 

coordinator-pattern ๊ธ€์—์„œ ์ถ”๊ฐ€์ ์ธ ์˜ˆ์‹œ๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ์ข‹์„๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

https://zeddios.medium.com/coordinator-pattern-bf4a1bc46930

 

Coordinator Pattern

Coordinator์˜ ์‹œ์ž‘๋ถ€ํ„ฐ ๊ฐ„๋‹จํ•œ ์‚ฌ์šฉ๊นŒ์ง€

zeddios.medium.com

 

 

 

 

 

 

 

 
๋Œ“๊ธ€