๊ฐœ๋ฐœ ์‹œํ–‰์ฐฉ์˜ค

[ iOS ์‹œํ–‰์ฐฉ์˜ค ] MVC ์•„ํ‚คํ…์ฒ˜ ํŒจํ„ด์„ ์ ์šฉํ•ด๋ณด์ž

Forest Yun 2024. 8. 8. 19:47
728x90

 
 

๐Ÿค” ๋ฐฐ๊ฒฝ & ๋ฌธ์ œ

<์•ผ๊ธฐ>์— ์—…๋ฐ์ดํŠธ๋ฅผ ์ง„ํ–‰ํ•  ์ˆ˜๋ก ๋ถ„๋ช…ํ•˜์ง€ ์•Š์€ ์ฝ”๋“œ ๊ธฐ์ค€์ด ํ”„๋กœ์ ํŠธ ๊ฐ€๋…์„ฑ ๋‚ฎ์ถ”๊ณ  ์žˆ์Œ์„ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
๋”๋ถˆ์–ด ์ผ๋ฐ˜ํ™”๋˜์ง€ ์•Š์€ View ๊ตฌํ˜„์œผ๋กœ, ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ผ๊ด€๋˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋น„ํšจ์œจ์ ์ธ ๋ฐ˜๋ณต ์ž‘์—…์ด ์žˆ์–ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋”๋ถˆ์–ด ๋‚ด์šฉ ๊ฒฐํ•ฉ๋„๋ฅผ ๊ฐ€์ง„ BottomMenuViewController์™€ ๊ฐ™์ด, ๊ฐ์ฒด ๊ฐ„ ๊ฒฐํ•ฉ๋„๊ฐ€ ๋†’์•„ ๊ธฐ๋Šฅ ๋ณ€๊ฒฝ ๋ฐ ๊ฐœ์„  ์‹œ ์ง„ํ–‰์˜ ์–ด๋ ค์›€์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
(๋‚ด์šฉ ๊ฒฐํ•ฉ๋„: ๊ฐœ์ฒด ๊ฐ„ ์ธํ„ฐํŽ˜์ด์Šค ์—†์ด ๋‚ด๋ถ€์— ์ง์ ‘ ์ ‘๊ทผํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ๊ด€๊ณ„)
 
 
 
 

๐Ÿ•ต๏ธ ๋ฌธ์ œ ํ•ด๊ฒฐ๋ฒ•

์•„ํ‚คํ…์ฒ˜ ํŒจํ„ด์„ ๋„์ž…ํ•ด, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์‹œ์Šคํ…œ ๊ตฌ์กฐ์™€ ์—ญํ• ์„ ๋ช…ํ™•ํžˆ ํ•˜์—ฌ ํ”„๋กœ์ ํŠธ์˜ ๊ฐ€๋…์„ฑ์„ ํ–ฅ์ƒ์‹œํ‚ค๊ณ  ๊ฐœ์ฒด ๊ฐ„ ์ ์ ˆํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ ์šฉํ•˜์—ฌ ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถ”๊ธฐ๋กœ ๊ฒฐ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ํŠนํžˆ View์˜ ์ผ๋ฐ˜ํ™”์— ์ง‘์ค‘ํ•˜์—ฌ ์žฌ์‚ฌ์šฉ์„ฑ ํ–ฅ์ƒ ๋ฐ ์ผ๊ด€๋œ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋ชฉํ‘œํ–ˆ์Šต๋‹ˆ๋‹ค.
 
 
 

 

โ‰๏ธ ์™œ MVC ํŒจํ„ด์ธ๊ฐ€

<์•ผ๊ธฐ>๋Š” ์•ฝ 35๊ฐœ์˜ ๊ฐ„๋‹จํ•œ ์‚ฌ์šฉ์ž ์Šคํ† ๋ฆฌ๋กœ ๊ธฐํš๋˜์–ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ทœ๋ชจ๊ฐ€ ์ž‘์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ถ”๊ฐ€์ ์ธ ํŒจํ„ด ์—†์ด MVC ํŒจํ„ด์„ ๋‹จ๋…์œผ๋กœ ์‚ฌ์šฉํ•ด๋„ ๊ดœ์ฐฎ์„ ๊ฒƒ์ด๋ผ ํŒ๋‹จํ–ˆ์Šต๋‹ˆ๋‹ค. ๋”ํ•ด ์ดํ›„ ์„œ๋น„์Šค๊ฐ€ ํ™•์žฅ๋˜์–ด ๊ทœ๋ชจ๊ฐ€ ์ปค์ง€๋”๋ผ๋„, MVC ํŒจํ„ด์€ ๋ชจ๋ธ๊ณผ ๋ทฐ์˜ ์—ญํ• ์ด ๋ถ„๋ช…ํ•˜๊ณ  ์—ฌ๋Ÿฌ ๋‹ค๋ฅธ ํŒจํ„ด๋“ค์ด MVC ํŒจํ„ด์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์•„ํ‚คํ…์ฒ˜ ๋ณ€๊ฒฝ์— ์šฉ์ดํ•  ๊ฒƒ ๊ฐ™์•„ MVC ์•„ํ‚คํ…์ฒ˜๋ฅผ ์„ ํƒํ–ˆ์Šต๋‹ˆ๋‹ค.
 
 
 
 

๐Ÿงš๐Ÿปโ€โ™€๏ธ ๋ฌธ์ œ ํ•ด๊ฒฐ ์‚ฌ๋ก€ 

๐Ÿ”—BookListViewController ์ฝ”๋“œ / ๐Ÿ”—AddingControlListView ์ฝ”๋“œ

  1. BookListViewController[C] - AddingControlListView[V], FetchedResultsController[V]
  2. AddingControlListView[C] - collectionView[V]
  3. FetchedResultsController[C] - Book[M]

 

 
 
ํ•˜๋‚˜์˜ ์•ฑ์€ ์—ฌ๋Ÿฌ MVC ํŒจํ„ด์ด ๊ฒฐํ•ฉ๋˜์–ด ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ํ•œ MVC ๊ตฌ์กฐ๊ฐ€ ํ•œ ํ™”๋ฉด์„ ๊ตฌ์„ฑํ•˜๊ธฐ ๋•Œ๋ฌธ์—, MVC ํŒจํ„ด๋Š” View์˜ ์ผ๋ถ€๋กœ ์—ฌ๊ฒจ์ง‘๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ AddingControlListView๋ฅผ ํ•˜๋‚˜์˜ (2) MVC ํŒจํ„ด์œผ๋กœ ๊ตฌ์กฐํ™”ํ•˜๊ณ , ๊ทธ๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์ง„ View๋ฅผ BookListController๊ฐ€ ํ†ต์ œ๋„๋ก ํ•˜์—ฌ ๋‹ค๋ฅธ (1) MVC ํŒจํ„ด์„ ๊ตฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.
 
์ฒ˜์Œ AddingControlListView๋Š” ์ปค์Šคํ…€ UICollectionView์˜€์Šต๋‹ˆ๋‹ค. ScrollViewDelegate๋ฅผ ํ†ตํ•ด Dragging์„ ์ธ์‹ํ•˜๊ณ  ์ ์ ˆํ•œ ํƒ€์ด๋ฐ์— completionHandler๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋กœ์ง์„ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ select ์ด๋ฒคํŠธ ๊ตฌํ˜„์„ ์œ„ํ•ด UICollectionViewDelegate๋ฅผ ์ฑ„ํƒํ•˜๊ฒŒ ๋˜๋ฉฐ, controller๊ฐ€ delegate๋ฅผ ์œ„์ž„๋ฐ›๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ž๊ธฐ ์ž์‹ ์ด ์œ„์ž„๋ฐ›๋Š” ๊ตฌ์กฐ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. delegate๊ฐ€ controller์™€์˜ ์†Œํ†ต์„ ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ธฐ์— ์ด๋Š” MVC ์•„ํ‚คํ…์ฒ˜์— ๋ถ€์ ํ•ฉํ•˜๋‹ค๊ณ  ํŒ๋‹จํ–ˆ์Šต๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ AddingControlListView๋ฅผ UIView๋กœ ํ™•์žฅํ•˜๊ณ , AddingControlListViewDelegate๋ฅผ ๊ตฌํ˜„ํ•ด select ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ์ž‘์—…์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ ListView๋Š” ๋‚ด๋ถ€์— collectionView๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋Œ€์ฒดํ–ˆ์œผ๋ฉฐ, UICollectionViewDelegate๋ฅผ ํ†ตํ•ด collectionView์™€ AddingControlListView๊ฐ€ ์†Œํ†ตํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.
 
BookListViewController์™€ Book ๋ชจ๋ธ์„ ์—ฐ๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋˜๋‹ค๋ฅธ (3) MVC ํŒจํ„ด์ด ๊ตฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.
Book ๋ชจ๋ธ์€ CoreData ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ํ†ตํ•ด ์ €์žฅ์†Œ์— ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜๊ณ  ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ CoreData์˜ results ๊ด€๋ฆฌ๋Š” FetchedResultsController๋ฅผ ํ†ตํ•ด ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. ํŠนํžˆ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€๊ฒฝ ๋  ๋•Œ๋งˆ๋‹ค Book ๋ชจ๋ธ์€ ์ด๋ฅผ Controller์—๊ฒŒ ์•Œ๋ฆฝ๋‹ˆ๋‹ค(Notification). Controller๋Š” Book ๋ชจ๋ธ์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  FetchesResultsDelegate๋ฅผ ํ†ตํ•ด ๋Œ€๋ฆฌ์ž์ธ BookListViewController์—๊ฒŒ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ์•Œ๋ ค ๋ชจ๋ธ์˜ ๊ฒฐ๊ณผ๋ฅผ View์— ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.
 
 
 
 

728x90