iOS

[ iOS ] MVC - 3 ) Controller ๊ฐ์ฒด

Forest Yun 2023. 11. 18. 20:56
728x90

 

 

Controller object๋ฅผ ์ฝ๊ณ  ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

 

Controller object

Retired Document Important: This document may not represent best practices for current development. Links to downloads and other resources may no longer be valid. Controller object A controller object acts as a coordinator or as an intermediary between one

developer.apple.com

 

 

 

Controller object

Controller ๊ฐ์ฒด๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ๋ทฐ ๊ฐ์ฒด์™€ ๋ชจ๋ธ ๊ฐ์ฒด ์‚ฌ์ด์˜ ์กฐ์ •์ž ๋˜๋Š” ์ค‘์žฌ์ž๋กœ์„œ ๋™์ž‘ํ•œ๋‹ค. MVC ๋””์ž์ธ ํŒจํ„ด์—์„œ Controller๋Š” view ๊ฐ์ฒด์—์„œ ์ด๋ฃจ์–ด์ง€๋Š” ์‚ฌ์šฉ์ž์˜ ๋™์ž‘๊ณผ ์˜๋„๋ฅผ ํ•ด์„ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์‚ฌ์šฉ์ž๊ฐ€ ๋ฒ„ํŠผ์€ tapํ•˜๊ฑฐ๋‚˜ ํด๋ฆญํ•  ๋•Œ, ๋˜๋Š” text field์— ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•  ๋•Œ์˜ ๊ทธ ๋™์ž‘๊ณผ ์˜๋„๋ฅผ ํ•ด์„ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ƒˆ๋กญ๊ฒŒ ๋˜๋Š” ๋ณ€ํ™”๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋ธ ๊ฐ์ฒด์— ์ „๋‹ฌํ•œ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ํŒŒ์ผ ์‹œ์Šคํ…œ์— ์ €์žฅ๋œ ๋ฌธ์„œ๋ฅผ ์—ด์–ด ๋ณผ ๋•Œ, ์ฆ‰ ๋ชจ๋ธ ๊ฐ์ฒด๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ Controller๋Š” ๊ทธ ์ƒˆ๋กœ์šด ๋ชจ๋ธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ทฐ ๊ฐ์ฒด์— ์ „๋‹ฌํ•œ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌ๋ฐ›์€ ๋ทฐ ๊ฐ์ฒด๋Š” ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ํ™”๋ฉด์— ํ‘œ์‹œํ•œ๋‹ค. ๋”ฐ๋ผ์„œ Controller๋Š” view ๊ฐ์ฒด๊ฐ€ model ๊ฐ์ฒด์˜, ๋ฐ˜๋Œ€๋กœ model ๊ฐ์ฒด๊ฐ€ view ๊ฐ์ฒด์˜ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์— ๋Œ€ํ•ด ์•Œ๊ฒŒ ํ•˜๋Š” ํŒŒ์ดํ”„์ด๋‹ค. ๋”๋ถˆ์–ด Controller๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๋Œ€ํ•œ ์ž‘์—…์„ ์„ค์ •ํ•˜๊ณ  ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋‹ค๋ฅธ ๊ฐ์ฒด์˜ LifeCycle์„ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

Cocoa ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ๋‹ค์Œ ์„ธ ๊ฐ€์ง€์˜ Controller ํƒ€์ž…์„ ์ œ๊ณตํ•œ๋‹ค:

  • Coordinator Controller: ์กฐ์ • ์ปจํŠธ๋กค๋Ÿฌ
  • View Controller(iOS): ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ
  • Mediating Controller(OS X, ์ง€๊ธˆ์˜ maxOS): ์ค‘์žฌ ์ปจํŠธ๋กค๋Ÿฌ

 

 

 

Coordinating Controller ์กฐ์ • ์ปจํŠธ๋กค๋Ÿฌ

Coordinating Controller๋Š” ์ „์ฒด ๋˜๋Š” ์ผ๋ถ€ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ž‘๋™์„ ๊ฐ๋…ํ•˜๊ณ  ๊ด€๋ฆฌํ•œ๋‹ค. ์ด controller๋Š” ๋ณดํ†ต ํŠน์ • ๋กœ์ง์ด ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์ฃผ์ž…๋˜๋Š” ์œ„์น˜์— ์žˆ๋‹ค. iOS ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ๋Š” ๋ณดํ†ต View Controller๊ฐ€ Coordinating Controller์˜ ์—ญํ• ์„ ํฌ๊ด„ํ•œ๋‹ค. macOS์—์„œ๋Š” NSWindowController ๋˜๋Š” NSDocumentController๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

Coordinating Controller๋Š” ๋‹ค์Œ์„ ํฌํ•จํ•œ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค:

  • delegation ๋ฉ”์„ธ์ง€์— ์‘๋‹ตํ•˜๊ณ  notification ๊ด€์ฐฐํ•˜๊ธฐ
  • ์‚ฌ์šฉ์ž๊ฐ€ ๋ฒ„ํŠผ์„ tapํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ด control์—์„œ ์ „๋‹ฌ๋˜๋Š” ์•ก์…˜ ๋ฉ”์„ธ์ง€์— ์‘๋‹ตํ•˜๊ธฐ
  • ๊ฐ์ฒด๋“ค ๊ฐ„ ์—ฐ๊ฒฐ ์„ค์ • ๋ฐ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด launch๋  ๋•Œ ๋“ฑ ์ด์™ธ ์„ค์ • ์ž‘์—… ์ˆ˜ํ–‰
  • "์†Œ์œ ํ•œ" ๊ฐ์ฒด์˜ ์ƒ๋ช…์ฃผ๊ธฐ ๊ด€๋ฆฌํ•˜๊ธฐ

 

 

 

View Controller ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ

iOS์˜ UIKit์™€ macOS์˜ AppKit๋Š” ๋ชจ๋‘ View Controller ํด๋ž˜์Šค๋“ค์„ ์ œ๊ณตํ•œ๋‹ค. ํ•˜์ง€๋งŒ ๋‘ View Controller๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ํŠน์ง•์„ ๊ฐ€์ง„๋‹ค.

macOS์˜ AppKit์—์„œ View Controller๋Š” NSViewController ํด๋ž˜์Šค์˜ ์ปค์Šคํ…€ ์„œ๋ธŒ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์ด๋‹ค. ์ด ์ปจํŠธ๋กค๋Ÿฌ๋Š” nib ํŒŒ์ผ์—์„œ ๋ณด๊ด€๋˜๋Š” view๋ฅผ ์†Œ์œ ํ•˜๋ฉฐ, view์˜ ํ•˜์œ„ view๋“ค์˜ ์—ฐ๊ฒฐ์„ ๊ด€๋ฆฌํ•˜๊ณ  ์—…๋ฐ์ดํŠธํ•œ๋‹ค.

iOS์˜ UIKit์—์„œ View Controller๋Š” ํ™”๋ฉด ๊ฐ€๋“ ์ปจํ…์ธ ๋ฅผ ํ‘œ์‹œํ•˜๋Š” view(๊ฐ€์žฅ ์ƒ์œ„์— ์œ„์น˜ํ•œ view, self.view)๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค. ์ด ์ปจํŠธ๋กค๋Ÿฌ๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ํ•ด๋‹น view์˜ Presentaion๊ณผ ๋’ค์ด์€ view์— ๋Œ€ํ•œ ํ™”๋ฉด ์ „ํ™˜์„ ๊ด€๋ฆฌํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋„ค๋น„๊ฒŒ์ด์…˜ ๋ฐ”์™€ ํƒญ๋ฐ”, ์ด์™ธ ๋‹ค๋ฅธ presentation ๋™์ž‘์€ View Controller์— ์˜ํ•ด ๊ด€๋ฆฌ๋˜๋ฉด ๊ตฌํ˜„๋œ๋‹ค. ๋”๋ถˆ์–ด View Controller๋Š” ๋ชจ๋‹ฌ View๋ฅผ ํ‘œ์‹œํ•  ์ˆ˜๋„ ์žˆ๊ณ , ๋ฉ”๋ชจ๋ฆฌ ๋ถ€์กฑ ๊ฒฝ๊ณ ์— ์‘๋‹ตํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ฐฉํ–ฅ์ด ๋ณ€๊ฒฝ๋  ๋•Œ์— view๋ฅผ ํšŒ์ „์‹œํ‚ฌ ์ˆ˜๋„ ์žˆ๋‹ค.

iOS์—์„œ View Controller๋Š” UIViewController์˜ ํ•˜์œ„ ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค์ด๋‹ค. UIKit๋Š” ํŠน์ • ๋ชฉ์ ์„ ๊ฐ€์ง„ ๋‹ค์–‘ํ•œ ์„œ๋ธŒ View Controller ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด UITableViewController๊ฐ€ ์žˆ๋‹ค.

View Controller๊ฐ€ Model๊ณผ view ์‚ฌ์ด์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ค‘์žฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” view์™€ controller์˜ ํ”„๋ ˆ์ž„์›Œํฌ ํด๋ž˜์Šค๋“ค์„ ํ™•์žฅํ•ด์•ผํ•œ๋‹ค.

View Controller๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์—ฌ๋Ÿฌ ํ”„๋ ˆ์ž„์›Œํฌ ํƒ€์ž…์— ๋Œ€ํ•ด delegate(์œ„์ž„์ž, ๋Œ€๋ฆฌ์ธ)๋‚˜ data source ๊ฐ์ฒด๋กœ ์‚ฌ์šฉ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด UICollectionView.delegate๋กœ View Controller๋ฅผ ํ• ๋‹นํ•˜์—ฌ CollectionView์˜ ์ž‘์—…์„ ๋Œ€์‹  ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

 

 

 

Mediating Controller ์ค‘์žฌ ์ปจํŠธ๋กค๋Ÿฌ (macOS)

Mediating Controller๋Š” View ๊ฐ์ฒด์™€ Model ๊ฐ์ฒด ๊ฐ„์˜ ๋ฐ์ดํ„ฐ ํ๋ฆ„์„ ์ˆ˜์›”ํ•˜๊ฒŒ ํ•œ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ View ๊ฐ์ฒด์— ํ‘œ์‹œ๋œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ๋•Œ, Mediating Controller๋Š” ์ž๋™์œผ๋กœ ๋ชจ๋ธ ๊ฐ์ฒด์— ์ƒˆ๋กœ์šด ๊ฐ’์„ ์ „๋‹ฌํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Model์˜ ํ”„๋กœํผํ‹ฐ๊ฐ€ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ๋•Œ, ์ปจํŠธ๋กค๋Ÿฌ๋Š” ์ ์ ˆํ•œ View ๊ฐ์ฒด๊ฐ€ ๊ทธ ๋ณ€๊ฒฝ๋œ ๊ฐ’์„ ๋ฐ˜์˜ํ•˜์—ฌ ํ™”๋ฉด์— ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ณด์ฆํ•œ๋‹ค. ๋‹ค๋ฅธ ์ปจํŠธ๋กค๋Ÿฌ๋“ค๊ณผ ๋‹ค๋ฅด๊ฒŒ Mediating Controller๋Š” ์žฌ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ด๋Ÿฌํ•œ ์ด์œ ์™€ ๋˜๋‹ค๋ฅธ ์ด์œ ๋กœ, Mediating Controller๋Š” Cocoa ๋ฐ”์ธ๋”ฉ ๊ธฐ์ˆ ์˜ ์ฃผ์š” ์š”์†Œ์ด๋‹ค.

๊ฐœ๋ฐœ์ž๋Š” ์ธํ„ฐํŽ˜์ด์Šค ๋นŒ๋” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ๋ถ€ํ„ฐ Mediating Controller๋ฅผ ๋“œ๋ž˜๊ทธํ•œ ํ›„ ์ปจํŠธ๋กค๋Ÿฌ์™€ ์ปจํŠธ๋กค๋Ÿฌ์˜ View, Model ๊ฐ์ฒด๋“ค ๊ฐ„์˜ ๋ฐ”์ธ๋”ฉ์„ ์„ค์ •ํ•  ๊ฐ์ฒด๋“ค์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

 

728x90