๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IOS๐ŸŽ/iOS+Swift

[iOS] View Controller ์ƒ๋ช…์ฃผ๊ธฐ

by Jouureee 2021. 5. 3.

๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋Š” ๋ฐ์ดํ„ฐ ๊ฐ์ฒด์™€ ๋ทฐ ์‚ฌ์ด์˜ ์ค‘๊ฐœ์ž์ด๋‹ค.

โ— method

viewDidLoad() 

- ๋ทฐ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋“œ ํ•œ ํ›„ ์ˆ˜ํ–‰, ๋ทฐ์˜ ์ดˆ๊ธฐํ™” ์ž‘์—… 

 

viewWillAppear()

- will : ํ™”๋ฉด์— ํ‘œ์‹œ๋˜๊ธฐ ์ง์ „ ํ˜ธ์ถœ

- ๋ทฐ๊ฐ€ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚  ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ

 

viewDidAppear()

- did : ํ™”๋ฉด์ด ํ‘œ์‹œ๋œ ์งํ›„ ํ˜ธ์ถœ

 

viewWillDisappear() 

- ๋ทฐ๊ฐ€ ํ™”๋ฉด์—์„œ ์‚ฌ๋ผ์งˆ ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ

 

viewDidDisappear()

- ๋ทฐ๊ฐ€ ํ™”๋ฉด์—์„œ ์™„์ „ํžˆ ์‚ฌ๋ผ์กŒ์„ ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ 


โ—์ƒ๋ช… ์ฃผ๊ธฐ(life cycle)

 

viewDidLoad

           ↓

viewWillAppear

           ↓

viewDidAppear

           ↓

viewWillDisappear

           ↓

viewDidDisappear

           ↓

viewDidUnload

 

 

loadView()๊ฐ€  controller๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” view๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ ์ด ๋ฉ”์†Œ๋“œ๋ฅผ ์ง์ ‘ ํ˜ธ์ถœํ•ด์„  ์•ˆ๋œ๋‹ค. ๋Œ€์‹ ์— viewController๊ฐ€ loadView()๋ฅผ view property๊ฐ€ ์š”์ฒญ๋˜์–ด์งˆ๋•Œ ํ˜ธ์ถœํ•˜๋Š”๋ฐ ์ด๋•Œ loadView()๋Š” view๋ฅผ ๋งŒ๋“ค๊ฑฐ๋‚˜ ๋กœ๋“œํ•˜๊ณ  view๋ฅผ view property์— ํ• ๋‹นํ•œ๋‹ค.


 

* viewWillApear ๊ณผ viewDidAppear ์‚ฌ์ด

  • viewWillLayoutSubviews()
  • viewDidLayoutSubviews()

๊ฐ€ ๋” ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

 


โ— ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๋ ˆ์ด์•„์›ƒ์ด ๊ฒฐ์ •๋˜๋Š” ๊ณผ์ •

  1. viewWillLayoutSubviews() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœ

  2. ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ์˜ ์ปจํ…ํŠธ ๋ทฐ๊ฐ€ layoutSubviews() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœ 

    - layoutSubviews() : ํ˜„์žฌ ๋ ˆ์ด์•„์›ƒ ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ƒˆ๋กœ์šด ๋ ˆ์ด์•„์›ƒ ์ •๋ณด๋ฅผ ๊ณ„์‚ฐ

  3. ๋ ˆ์ด์•„์›ƒ ์ •๋ณด ๋ณ€๊ฒฝ๋œ ์‚ฌํ•ญ๋“ค์„ ๋ทฐ์— ๋ฐ˜์˜ 

  4. viewDidLayoutSubviews() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœ

 

๋Œ“๊ธ€