ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Review of Connect the UI to Code (1) - Food Tracker
    iOS/๐Ÿค– App 2021. 1. 18. 21:58

    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

    IUO์— nil ์ด ํ• ๋‹น(๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ) ๋œ ํ›„์˜ ์ ‘๊ทผ์€ ๋Ÿฐํƒ€์ž„ ์ค‘ Fatal Error์„ ์ผ์œผํ‚จ๋‹ค.

     

    ์ ‘๊ทผ ๊ฐ€๋Šฅ ์‹œ์ 

     

    " 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

    ๋Œ“๊ธ€

์–ด์ œ๋ณด๋‹ค ๋ฐœ์ „ํ•œ ๋‚˜