Review of Connect the UI to Code (1) - Food Tracker
Connect the UI to Source Code
์คํ ๋ฆฌ ๋ณด๋์ ์์๋ค ( Views ) ์ ์์ค ์ฝ๋์ ์ฐ๊ฒฐ๋์ด ์์ต๋๋ค.
์คํ ๋ฆฌ๋ณด๋์์ ํ๋์ ์ฌ (scene)์ ์ฝํ ์ธ ๋ก ์ด๋ฃจ์ด์ง ํ๋์ ํ๋ฉด์ ๋ํ๋ด๋ฉฐ ๋ณดํต ํ๋์ View Controller์ ์ฐ๊ฒฐ๋์ด ์์ต๋๋ค.
View controller ์์ ์ฑ์ ๋์์ ๊ตฌํํฉ๋๋ค.
View controller๋ ํ๋์ ์ปจํ ํธ ๋ทฐ (content view)์ ๊ทธ ๋ฐ ๊ณ์ธต์ ์๋ธ ๋ทฐ( subviews )๋ฅผ ๊ด๋ฆฌํฉ๋๋ค.
์ด๊ฒ์ Content View Controllers์ ๊ฒฝ์ฐ์ ๋๋ค.
Container View Controllers๋ ์ฌ๋ฌ ๊ฐ์ ์์ View Controller๋ฅผ ๊ด๋ฆฌํฉ๋๋ค.
๊ด๋ จ ๊ธ: View Controller์ ์ญํ
View controllers ๊ฐ ํ๋ ์ผ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
1. ์ฑ์ data model (์ฑ์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง๊ณ ์๋)๊ณผ ๋ฐ์ดํฐ๋ฅผ ํ๋ฉด์ ๋ฟ๋ ค์ฃผ๋ view ๊ฐ์ ์ ๋ณด ํ๋ฆ์ ์กฐ์จํฉ๋๋ค
2. ์ฝํ ์ธ ๋ทฐ์ ์๋ช ์ฃผ๊ธฐ๋ฅผ ๊ด๋ฆฌํฉ๋๋ค
3. ๊ธฐ๊ธฐ๊ฐ ํ์ ํ์์ ๋ ํ๋ฉด ๋ฐฉํฅ์ ๊ด๋ฆฌํฉ๋๋ค.
4. ์ฑ ๋ด ์ฌ์ ์์น(navtigation)๋ฅผ ์ ํด์ค๋๋ค.
5. ์ ์ ์ ๋์์ ๋ฐ๋ฅธ ํ๋์ ๊ตฌํํฉ๋๋ค. (์๋ฅผ ๋ค๋ฉด touchInsideOut ์ด๋ฒคํธ)
iOS์์์ ๋ชจ๋ view controller๋ UIViewController ํด๋์ค์ด๊ฑฐ๋ ์ด ํด๋์ค๋ฅผ ์๋ธ ํด๋์ฑ ํ ํด๋์ค์ ๋๋ค.
์คํ ๋ฆฌ๋ณด๋์ ์ฌ๊ณผ View Controller๋ฅผ ์ฐ๊ฒฐํ๋ ๋ฐฉ๋ฒ์ Identiy inspector์์ ์ค์ ํ ์ ์์ต๋๋ค.
์๋ ์ฌ์ง์ฒ๋ผ XCode ์ผ์ชฝ์ Document Outline์์ View Controller๊ฐ ์ ํ๋์ด ์์์ ํ์ธํ ๋ค, ์ค๋ฅธ์ชฝ Inspector Bar์์ ๋ช ํจ ๋ชจ์์ Identity Inspector๋ฅผ ํด๋ฆญํ๋ฉด Custom Class์์ ์ฝ๋์ ์ฐ๊ฒฐํ ์ ์์ต๋๋ค.
๋ฐํ์์ ์คํ ๋ฆฌ๋ณด๋๋ ViewController ํด๋์ค์ ์ธ์คํด์ค๋ฅผ ๋ง๋ญ๋๋ค.
ViewController๋ ์์ ์ค๋ช ํ์๋ฏ์ด UIViewController๋ฅผ ์๋ธ ํด๋์ฑ ํ custom view controller ํด๋์ค์ ๋๋ค.
ํ์ง๋ง ์ฌ์ด view controller ํด๋์ค์ ์ฐ๊ฒฐ๋์๋ค๊ณ ํด์, ์ฑ ๋ด interaction์ด ๋ฐ๋ก ๊ฐ๋ฅํ ๊ฒ์ด ์๋๋๋ค.
๋ฐฉ๊ธ ์ ์ ํ connection์ ์ฌ๊ณผ View Controller์ connection์ด์์ต๋๋ค.
์ถ๊ฐ์ ์ผ๋ก ์ฌ ๋ด๋ถ์ view ๋ค๊ณผ view controller ๊ฐ์ connection ์ด ํ์ํฉ๋๋ค.
์ด๊ฒ๋ค์ outlets & actions๋ก ๋ถ๋ฆฝ๋๋ค.
Create Outlets for UI Elements
Outlet์ view ์ธ์คํด์ค์ ์ฐธ์กฐ๋ฅผ View controller์ ์ ์ฅํด ๋๋ ๊ฒ์ ๋๋ค.
์๋ฅผ ๋ค์ด, outlet์ UITextField ์ธ์คํด์ค์ ์ฐธ์กฐ๋ฅผ ์ ์ฅํด๋๊ณ ๊ทธ ์ธ์คํด์ค ์์ฑ๋ค์ ํน์ ์์ ์ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
Outlet์ ์ค์ ํ๋ ๋ฐฉ๋ฒ์ ์ ํด์ ธ ์์ต๋๋ค.
๊ฐ์ฅ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ Storyboard์ Assistant Editor๋ฅผ ์ผ๋์ ์ํ์์ ctrl ํค๋ฅผ ๋๋ฅธ ์ํ๋ก ์ฌ ๋ด๋ถ์ view๋ฅผ ๋์ด์ View Controller ํ์ผ์ ๋ฃ๋ ๊ฒ์ ๋๋ค.
๊ด๋ จ ๊ธ: Assistant Editor
UITextField๋ฅผ View Controller์ ๋ฃ๋ ์ํฉ์ด๋ผ๊ณ ๊ฐ์ ํ์ ๋, ์ด ๋์์ ํ๋ฉด ์๋ ์ฌ์ง๊ณผ ๊ฐ์ ํ์ ์ด ๋จ๊ฒ ๋ฉ๋๋ค.
Syntax
@IBOutlet weak var nameTextField: UITextField!
Outlet ์ฐ๊ฒฐ์ @IBOutlet Attribute๋ฅผ ์์ ๋ถ์ฌ ์ค๋๋ค.
weak ํค์๋ ๋๋ถ์, view ์ธ์คํด์ค์ ๋ ํผ๋ฐ์ค ์นด์ดํ ์ ์ฆ๊ฐํ์ง ์์ ๊ฒ์ ๋๋ค.
view์ ๋ฉ๋ชจ๋ฆฌ ํด์ ์ ์๋์ผ๋ก nil๋ก ์ด๊ธฐํ๋ฉ๋๋ค.
๊ด๋ จ ๊ธ: Weak Reference
ํ์ ์ UITextField! ๋ก์, IUO๋ฅผ ์ฌ์ฉํ์๊ธฐ ๋๋ฌธ์ ๋ฐํ์์ view ์ธ์คํด์ค์ ๋ฉ๋ชจ๋ฆฌ ํด์ ํ ์ ๊ทผ ์ ์ฑ ํฌ๋์๊ฐ ๋ฐ์ํ ๊ฒ์ ๋๋ค.
ํ์ง๋ง `nameTextField`์ Superview ๊ฐ `nameTextField`๋ฅผ ๊ฐํ ์ฐธ์กฐ ํ๊ณ ์๊ธฐ ๋๋ฌธ์, Superview๊ฐ ๋ฉ๋ชจ๋ฆฌ ํด์ ๋์ง ์๋ ํ ๊ทธ๊ฒ์ Subview๋ค์ด ๋ฉ๋ชจ๋ฆฌ ํด์ ๋๋ ์ํฉ์ ์์ ๊ฒ์ ๋๋ค.
์ถ๊ฐ์ ์ผ๋ก, ์ฌ๊ธฐ์ SuperView๋ StackView์์ ํ์ธํ ์ ์์ต๋๋ค. SuperView๋ ๋ VC์ Content view๊ฐ ๊ฐํ ์ฐธ์กฐ ํ๊ณ ์์ต๋๋ค!
(์ด๊ฒ์ด ๋ทฐ ๊ณ์ธต์ ๋๋ค!)
์์์ ์ธ ์ํฉ์์, ํด์ ํ ํฌ๋์๊ฐ ๋ฐ์ํ๋ ๊ฒ์ ์ง์ ํ์ธํด ๋ณด์์ต๋๋ค.
๊ด๋ จ ๊ธ: Optional Unwrapping
์ ๊ทผ ๊ฐ๋ฅ ์์
" viewDidLoad() ๋ฐ๋์์๋ถํฐ "
์คํ ๋ฆฌ๋ณด๋์์ VC๊ฐ ๋ก๋๋ ๋, ์์คํ ์ ๋ทฐ ๊ณ์ธต์ ์ธ์คํด์ค ํํ๊ณ ์ ์ ํ ๊ฐ๋ค์ VC์ outlet์ ์ ์ฅํฉ๋๋ค.
์ด ๋ฉ์๋๋ฅผ ์คํํ๋ ์์ ์์ ๋ชจ๋ outlet๋ค์ ์ด๊ธฐํ๋์ด์์ต๋๋ค. ์์ ํ๊ฒ ์ ๊ทผํ ์ ์์ต๋๋ค.
Actions ๋ฐ ์ดํ ๋ฆฌ๋ทฐ๋ ๋ค์ ๊ธ์ ๋ค๋ฃจ๊ฒ ์ต๋๋ค!
๋!
๋ค์ ๊ธ:Review of Connect the UI to Code (2) - Food Tracker
Review of Connect the UI to Code (2) - Food Tracker
Define an Action to Perform iOS ์ฑ์ ์ด๋ฒคํธ ๊ธฐ๋ฐ ํ๋ก๊ทธ๋๋ฐ ( event-driven programming ) ๊ธฐ๋ฐ์ ๋๋ค. ์ด๊ฒ์ ์ฑ์ ์คํ ํ๋ฆ์ด ์ด๋ฒคํธ์ ์ํด ์ ํด์ง๋ค๋ ์๋ฏธ์ ๋๋ค. ์ด๋ฒคํธ์๋ ์์คํ ์ด ๋ฐ์์ํค๋ ์ด.
woozzang.tistory.com