[iOS] App States & View Life Cycle | ์•ฑ์˜ ์ƒํƒœ์™€ ๋ทฐ์˜ ์ƒ๋ช…์ฃผ๊ธฐ
๐ŸŽ iOS

[iOS] App States & View Life Cycle | ์•ฑ์˜ ์ƒํƒœ์™€ ๋ทฐ์˜ ์ƒ๋ช…์ฃผ๊ธฐ

์ƒ๋ช…์ฃผ๊ธฐ(Lifecycle)์ด๋ž€ ๋ฌด์—‡์ธ๊ฐ€?

์–ธ์ œ ํŠน์ • ๋กœ์ง์„ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฅผ ์–ด๋””์„œ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค๋ผ๋Š” ๋ฌธ์ œ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ๋Š” ๊ฐœ๋…

 

 

App States

 

  • Not running: ์•ฑ์ด ์‹คํ–‰๋˜์ง€ ์•Š๊ฑฐ๋‚˜ ์ข…๋ฃŒ๋œ ์ƒํƒœ
  • Foreground: ํ™”๋ฉด์ด ๋‚˜ํƒ€๋‚œ ์ƒํƒœ
    • inActive: ์ƒํ˜ธ์ž‘์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ์ƒํƒœ, ํ™”๋ฉด์€ ๋ณด์ž„ ex) ์ „ํ™”๊ฐ€ ์™”์„ ๋•Œ, ์‹œ์Šคํ…œ ๋ฉ”์‹œ์ง€๊ฐ€ ๋–ด์„ ๋•Œ ('๋ฐฐํ„ฐ๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค')
    • Active: ์ƒํ˜ธ์ž‘์šฉ์ด ๊ฐ€๋Šฅํ•œ ์ƒํƒœ, ์•ฑ์ด ์‹คํ–‰๋˜๊ณ  ์žˆ๋Š” ์ƒํƒœ
  • Background: ํ™”๋ฉด์ด ์‚ฌ๋ผ์ง„ ์ƒํƒœ
    • ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ƒํƒœ์—์„œ ์‹ค์งˆ์ ์ธ ๋™์ž‘์„ ํ•˜๊ณ  ์žˆ๋Š” ์ƒํƒœ ex) ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ, ์œ ํŠœ๋ธŒ ์Œ์•… ์‹คํ–‰
  • Suspended: ๋ญ”๊ฐ€ ์žก์•„๋‘” ์ƒํƒœ
    • ๋น ๋ฅธ ์žฌ์‹คํ–‰์„ ์œ„ํ•ด ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ตœ์†Œํ•œ์œผ๋กœ๋งŒ ์žก๊ณ (ํ• ๋‹นํ•˜๊ณ ) ์žˆ์Œ
    • ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•˜๋ฉด ์‹œ์Šคํ…œ์ด Suspended ์ƒํƒœ์˜ ์•ฑ์„ ์ข…๋ฃŒ์‹œํ‚ด
    • iOS๋Š” App switcher์ƒํƒœ์—์„œ ์•ฑ์„ ๊ฐ•์ œ ์ข…๋ฃŒํ•˜๋Š” ๊ฒƒ์ด ๋” ๋ฐฐํ„ฐ๋ฆฌ๋ฅผ ์†Œ๋ชจํ•จ -> Suspended ์ƒํƒœ ์œ ์ง€ํ–ˆ๋‹ค๊ฐ€ ์•ฑ ํ‚ค๋Š”๊ฒŒ ๋” ๋‚˜์Œ

 

 

View Life Cycle

 

 

1. loadView

: ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ๋ฉ”์ธ ๋ทฐ๋ฅผ ๋กœ๋“œ ํ•  ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ, ์Šคํ† ๋ฆฌ๋ณด๋“œ๋ฅผ ์“ฐ๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ์Šคํ† ๋ฆฌ๋ณด๋“œ์— ์žˆ๋Š” ๋ทฐ๋ฅผ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ตณ์ด ํ•„์š”ํ•˜์ง€ ์•Š์Œ. xib(nib)์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉ.

 

2. viewDidLoad

: ๋ทฐ๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์— ์ƒ์„ฑ๋œ ํ›„์— ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ. 

 

3. viewWillAppear

: ๋ทฐ๊ฐ€ ๊ณ„์ธต์— ์ถ”๊ฐ€๋˜๊ณ , ํ™”๋ฉด์— ํ‘œ์‹œํ•˜๊ธฐ ์ง์ „์— ์ˆ˜ํ–‰.

 

4. viewWillLayoutSubviews

: ๋ทฐ์˜ bounds๊ฐ€ ๋ณ€ํ•˜๋ฉด ๋ทฐ๋Š” ํ•˜์œ„ ๋ทฐ๋“ค์˜ ์œ„์น˜๋ฅผ ์กฐ์ •ํ•˜๋Š”๋ฐ, ๋ ˆ์ด์•„์›ƒ์ด ๊ฒฐ์ •๋˜๊ธฐ ์ „ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉ, ์˜คํ† ๋ ˆ์ด์•„์›ƒ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์„œ๋ธŒ๋ทฐ์˜ ๋ ˆ์ด์•„์›ƒ์„ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์ ํ•ฉ

  • ๋ทฐ ์ถ”๊ฐ€ / ์ œ๊ฑฐ
  • ๋ทฐ ํฌ๊ธฐ / ์œ„์น˜ ์—…๋ฐ์ดํŠธ
  • Layout constraint ์—…๋ฐ์ดํŠธ
  • Main View์˜ SubView Load (UITableView, UICollectionView...)

 

5. viewDidLayoutSubviews

: ๋ ˆ์ด์•„์›ƒ์ด ๊ฒฐ์ •๋˜๊ณ  ๋‚˜์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉ, ์„œ๋ธŒ๋ทฐ๊ฐ€ ์…‹ํŒ…๋œ ํ›„ ๋ณ€๊ฒฝํ•  ์ ์ด ์žˆ๋‹ค๋ฉด ์ด ๋•Œ ์ˆ˜์ •ํ•˜๊ธฐ์— ์ ํ•ฉ

  • ๋‹ค๋ฅธ ๋ทฐ๋“ค์˜ Content ์—…๋ฐ์ดํŠธ
  • ๋ทฐ๋“ค์˜ ํฌ๊ธฐ๋‚˜ ์œ„์น˜๋ฅผ ์ตœ์ข…์ ์œผ๋กœ ์กฐ์ •
  • table์˜ ๋ฐ์ดํ„ฐ reload

6. viewDidAppear

: ๋ทฐ๊ฐ€ ํ™”๋ฉด์— ํ‘œ์‹œ๋œ ์ดํ›„์— ์ˆ˜ํ–‰, ๋ทฐ๋ฅผ ๋ณด์—ฌ์ค„ ๋•Œ ํ•„์š”ํ•œ ์ถ”๊ฐ€์ ์ธ ์ž‘์—… ๋‹ด๋‹น

 

7. viewWillDisappear

: ๋ทฐ๊ฐ€ ๊ณ„์ธต์—์„œ ์‚ฌ๋ผ์ง€๊ธฐ ์ง์ „์— ์ˆ˜ํ–‰. ๋ทฐ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋‚˜์„œ ํ–ˆ๋˜ ํ–‰๋™๋“ค์„ ๋˜๋Œ๋ฆฌ๊ฑฐ๋‚˜ ์ •๋ณด๋ฅผ ์‚ญ์ œํ•˜๊ธฐ ์ „ ์ €์žฅํ•ด๋‘˜ ๋•Œ ์œ ์šฉ

 

8. viewDidDisappear

: ๋ทฐ๊ฐ€ ๊ณ„์ธต์—์„œ ์‚ฌ๋ผ์ง„ ๋’ค ์ˆ˜ํ–‰. 

 

 

 

์œ„์™€ ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉ ์‹œ์—๋Š” override ํ‚ค์›Œ๋“œ์™€ super ํ‚ค์›Œ๋“œ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ๋ถ™์—ฌ์ค€๋‹ค. 

 

 

 

 

 

viewDidLoad( ) vs. loadView( ) (Swift3)

This month at Flatiron School we are knee-deep in “project mode”! For my group’s project, I have been using programmatic UI elements in our…

medium.com


 

 

 

App Delegate & Scene Delegate

iOS 13๋ถ€ํ„ฐ AppDelegate์— ์žˆ๋Š” ํ•จ์ˆ˜๋“ค์„ SceneDelegate์—์„œ ๊ด€๋ฆฌํ•ด์ฃผ๊ธฐ ์‹œ์ž‘ํ–ˆ๋‹ค.

 

๊ณผ๊ฑฐ(~iOS 12) LifeCycle

 

ํ˜„์žฌ(iOS 13~) LifeCycle

Window์˜ ๊ฐœ๋…์ด Scene์œผ๋กœ ๋Œ€์ฒด๋˜๋ฉด์„œ ํ•˜๋‚˜์˜ ์•ฑ์—์„œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ Scene์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. ์˜ค์˜ค~

์•„๋ž˜ ์‚ฌ์ง„์ฒ˜๋Ÿผ multiple instance๋ฅผ ํ•จ๊ป˜ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๊ฐœ๋…์„ ๋ณ€๊ฒฝํ•œ๊ฑด๊ฐ€๋ด„

 

 

 

๊ด€๋ จ ๋ฌธ์„œ: 

 

Apple Developer Documentation

 

developer.apple.com

 

 

Architecting Your App for Multiple Windows - WWDC19 - Videos - Apple Developer

Dive into the details about what it means to support multitasking in iOS 13. Understand how previous best practices fit together with new...

developer.apple.com

 

Apple Developer Documentation

 

developer.apple.com