์์ ํฌ์คํ ํ ๊ธ ์ค์ Coordinator๋ฅผ ์ฌ์ฉํ ํ๊ธฐ๊ฐ ์๋ค.
๋๋งบ์์
์๋ฌด๋๋ ์ง๊ธ๊น์ง ์๋ชป ์ฌ์ฉํ๊ณ ์๋ค๊ณ ์ ์์๋๋ฐ
ํ๋ก์ ํธ๊ฐ ๊ฑฐ์ ๋๋๊ฐ๋ฉด์ ๋ฆฌํฉํ ๋ง์ ์์ํ๋ค.
์์ ํฌ์คํ ์ฒ๋ผ MVVM + CleanArchitecture๋ฅผ ์ ์ฉํ๋ฉด์ ํจ๊ป Coordinator๋ฅผ ์ ์ฉํ์๋ค.
๋น์์๋ ViewModel์ ์์ฑ์์ Coordinator๋ฅผ ๋ฃ์ด์ฃผ๊ณ
ํด๋น ViewModel์์ Coordinator์ ์ง์ ์ ๊ทผํด ํ๋ฉด์ ํ์ ํ๊ณ ์์๋ค.
์ด๋ ๊ฒ ์งํํ๋ค ๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค.
1. ์์ฑ์๋ก ์ ๋ ฅ๋ฐ์ ์์ Coordinator ํ๋กํ ์ฝ์ ๋ฐ์์ผ๋ง ํ๊ณ , ์ดํ ํ์ (์ด๊ฑฐํ)์ ์ ๊ทผํด ๊ฐ์ง๊ณ ์๋ Coordinator์ ๋์ ํด์ผ ํ๋ค.
2. ์์ ๋ฌธ์ ๋ก ์ธํด ํน์ ํ ๋ทฐ์ ๋ทฐ๋ชจ๋ธ์ด ๋ค๋ฅธ Coordinator์์ ๋์ํ ๋, Coordinator๊ฐ ์ด๋ค Coordinator์ธ์ง ํ์ธํด์ผ ํ๋ฉด์ ํ์ ํ ์ ์๊ฒ ๋๋ค.
3. ๋๋ฌด๋ ๋ฐฉ๋ํ ํ๋ฉด์ ํ ์ฝ๋๊ฐ ๊ตฌ๋ถ์ด ๋์ง ์๊ณ ๋์ํ๊ฒ ๋๋ฏ๋ก ์ฝ๋ ์์ ์ ์ด๋ ค์์ด ์๊ธด๋ค.
์์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
func showNotificationController() {
if coordinator?.type == .home {
(coordinator as? HomeCoordinator)?.showNotificationController()
} else {
(coordinator as? CommunityCoordinator)?.showNotificationController()
}
}
ํน์ ํ ๋ทฐ๋ชจ๋ธ์์ NotificationController๋ก ํธ์ํ๊ธฐ ์ํด ์์ฑํ ์ฝ๋ ์์์ด๋ค.
๋งํ ๊ฒ์ฒ๋ผ
์์ฑ์๋ก Coordinator๋ฅผ ๋ฐ๊ธฐ ๋๋ฌธ์ ์ฌ์ ์ ์ ์ํด ๋ ์ด๊ฑฐํ์ ํตํด ์ด๋ค Coordinator์ธ์ง ํ์ธํ๊ณ ํ๋ฉด์ ํ์ ์งํํด์ผ ํ๋ค.
์ด์ ๊ฐ์ ๋ฌธ์ ๋ฅผ Delegate ํจํด์ ๋์ ํด ํด๊ฒฐํด ๋ณด๊ธฐ๋ก ํ๋ค.
1. ์์ฑ์์๋ Coordinator๊ฐ ์๋๋ผ delegate ํ๋กํ ์ฝ ๋ฐ๊ธฐ
2. ์ด๋ ๊ฒ ๋๋ฉด ํ๋ฉด์ ํ ์ ํ์ ์ ํ์ธํ ํ์ ์๋ค.
3. ๋ฐฉ๋ํ ์ฝ๋ ๋ฌธ์ ๋ ์ฌ์ ํ์ง๋ง ๊ตฌ๋ถ์ด ๊ฐ๋ฅํด์ง๋ค. extension์ ํตํด Coordinator ๋ด๋ถ์์ ํน์ ํ ViewModel์ ์์ฑ์์ธ delegate๋ฅผ ์ฑํํ๊ณ ์ฝ๋๋ฅผ ์์ฑํ๋ค. ์ด๋ ๊ฒ ๋๋ฉด ์ต์ํ์ ๊ตฌ๋ถ์ ๊ฐ๋ฅํด์ง๋ฉฐ ์กฐ๊ธ์ด๋๋ง ์ฝ๋ ์์ ์ ๋์์ด ๋์๋ ๊ฒ ๊ฐ๋ค.
์์ ํ ์ฝ๋์์๋ ๋ค์๊ณผ ๊ฐ๋ค.
// Protocol
protocol NotificationViewDelegate {
func showNotificationViewController()
}
// Coordinator
extension HomeCoordinator: NotificationViewDelegate {
func showNotificationViewController() {
...
}
}
// ViewModel
init(..., delegate: NotificationViewDelegate) {
self.delegate = delegate
}
func showNotificationViewController() {
delegate?.showNotificationViewController()
}
์ด๋ ๊ฒ ๋ฆฌํฉํ ๋ง์ ์ฐ์ ์ ์ผ๋ก ์งํํ๋ค.
์ด๋ ๊ฒ ์ฝ๋๋ฅผ ๋ฆฌํฉํ ๋งํ๋๋ผ๋ ๋ฌธ์ ๊ฐ ๋จ์์๋ค.
์ฌ๋ฌ Coordinator์์ ๋์ผํ ์ฝ๋๊ฐ ์์ฑ๋๋ ๋ฌธ์ ๋ ์ฌ์ ํ ์กด์ฌํ๊ธฐ ๋๋ฌธ์ด๋ค.
๋, ์ด์ ๋ณ๊ฐ๋ก Coordinator์์ ์์ฑ๋๋ ์ธ์คํด์ค๋ค์ด ๋ฉ๋ชจ๋ฆฌ์์ ํด์ ๋์ง ์๊ณ ๋์ ๋๋ ์ด์๋ ์๋ค.
์๋ฌด๋๋ ์์กด์ฑ ์ฃผ์ ๊ณผ ๊ด๋ จ๋ ์ฝ๋๊ฐ ์๊ธฐ๋ ํ๊ณ , repository, usecase ๋ฑ์ ์ง์ Coordinator์์ ์์ฑํด ์ฃผ์ ํ๊ณ ์๊ธฐ ๋๋ฌธ์ ๋ฐ์ํ๋ ๋ฌธ์ ์ธ ๊ฒ ๊ฐ๋ค.
์ด ๋ ๋ฌธ์ ๋ฅผ ๊ฐ์ด ํด๊ฒฐํ ์์ด๋์ด๊ฐ ์๊ธฐ๋ฉด ๋ฆฌํฉํ ๋งํ๊ณ , ํฌ์คํ ํด์ผ๊ฒ ๋ค.
'iOS > Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ReactorKit] ํ ์คํธ ์ฝ๋ ์์ฑํด๋ณด๊ธฐ (feat.expectation) (0) | 2024.07.15 |
---|---|
[Swift] ๋ฉ๋ชจ๋ฆฌ ๋์๋ฅผ ํด๊ฒฐํด๋ณด์(feat.map) (0) | 2024.02.20 |
[iOS] UITableView์์ ์ ์ค์ฒ ์ฌ์ฉํ๊ธฐ / ์ด์์ ๋ฆฌ (1) | 2023.07.20 |
[Swift] Extension์ ๋ํด์ ์์๋ณด์. (1) | 2023.04.13 |
[Swift] Property(ํ๋กํผํฐ)์ ๋ํด์ ์์๋ณด์. (0) | 2023.04.08 |