ViewController ์๋ช ์ฃผ๊ธฐ๋ ViewController๊ฐ ์์ฑ๋๊ณ ์ฌ๋ผ์ง๋ ๋ฑ ํ๋ฉด์ ์ ํ ์์ ์ ํจ์๊ฐ ํธ์ถ๋๋ ๊ณผ์ ์ด๋ค.
์ด๋ฅผ ์ดํดํ๋ฉด ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ, ๋ฐ์ดํฐ ์ ๋ฐ์ดํธ, UI๋ณ๊ฒฝ ๋ฑ์ ํจ๊ณผ์ ์ผ๋ก ํ ์ ์๋ค.
UIVIewController๋ก ์ ์ ๋์ด ์๋๋ฐ ์ด์ ๋ ์๋ช ์ฃผ๊ธฐ์ ๋ชจ๋ ๋ฉ์๋๋ UIKit ๋ด์ UIViewController๋ผ๋
ํด๋์ค ์์ ์ด๋ฏธ ์ ์ ๋์ด ์๊ธฐ ๋๋ฌธ์, UIViewController ํด๋์ค๋ฅผ ์์ ๋ฐ๋ ๋ทฐ์ปจํธ๋กค๋ฌ์์ ์๋ช ์ฃผ๊ธฐ ๋ฉ์๋๋ค์
override ํ ๋ค์ ์ฌ์ฉํ๋ฉด ๋๋ค.
๊ฐ ์๋ช ์ฃผ๊ธฐ ๋ฉ์๋๋ ์ด์ ๋จ๊ณ๊ฐ ์๋ฃ๋๊ธฐ ์ ์๋ ํธ์ถ๋์ง ์๋๋ค.
loadView
๋ทฐ๋ฅผ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ฆฌ๋ ๊ณผ์ ์ผ๋ก ํจ๋ถ๋ก ํธ์ถํด์ ์ฌ์ฉํ๋ฉด ์๋๋ค.
์ปจํธ๋กค๋ฌ๊ฐ ๊ด๋ฆฌํ๋ ๋ทฐ๋ฅผ ๋ง๋๋ ์ญํ ์ ํ๋ค.
viewDidLoad ๋ฉ์๋ ์ด์ ์ ํธ์ถ๋์ง๋ง ๊ณต์๋ฌธ์์์๋ ํด๋น ๋ฉ์๋๋ฅผ ์ง์ ํธ์ถํด์ ์ฌ์ฉํ๋ ๊ฒ์ ์ง์ํ๊ณ ์๋ค.
๊ณผ์ ํ๋ฉด์ loadView๋ฅผ ์ฌ์ฉํ ๋ด์ฉ์ด ๊ถ๊ธํ๋ค๋ฉด..
https://dkswlgus0314.tistory.com/230
viewDidLoad
์คํ ๋ฆฌ๋ณด๋์์ ์ฐ๊ฒฐ๋ค๊ณผ์ ์ฐ๊ฒฐ์ด ๋๋ ์์
์ฝ๋๋ก UI๋ฅผ ์งค ๋ ์คํ ๋ฆฌ๋ณด๋๋ก ๋ง๋ค์ด์ง ํ๋ฉด์ด ์๋๋ผ ์ฝ๋๋ก ์ง์ฌ์ง ํ๋ฉด์ ๋ถ๋ฌ์ค๊ธฐ ์ํ ํจ์
ViewController์ view ๊ณ์ธต์ด ๋ฉ๋ชจ๋ฆฌ์ ๋ก๋๋ ํ(๋ทฐ๊ฐ ์์ฑํ๊ณ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ฆด ๋)์ธ
๋ทฐ๊ฐ ํ๋ฉด์ ๋ํ๋๊ธฐ ์ ์ ํ ๋ฒ๋ง ํธ์ถ๋๋ค.
๋ฃจํธ๋ทฐ ํ์์ ๋ทฐ๋ค์ ์ถ๊ฐํ๊ณ ์ ์ฝ์ ์ค์ ํ๋ ์ฝ๋๊ฐ ์ถ๊ฐ๋จ
view์ ๋ก๋ฉ์ด ์๋ฃ๋์์ ๋ ์์คํ ์ ์ํด ์๋์ผ๋ก ํธ์ถ๋๋ฏ๋ก
๋ฆฌ์์ค๋ ํ๋ฉด์ ๊ตฌ์ฑ์์๋ค์ ์ด๊ธฐํ ํ๋ ์ฉ๋๋ก ์ฌ์ฉ.
์ด๊ธฐ UI ์ค์ , ๋ฐ์ดํฐ ๋ก๋, ์๋ธ๋ทฐ ์ถ๊ฐ ๋ฑ์ ์์ ์ ์ํํ๋ค.
override func viewDidLoad() {
super.viewDidLoad()
// ์ด๊ธฐ UI ์ค์
setupUI()
// ๋ฐ์ดํฐ ๋ก๋
loadData()
// ๊ธฐํ ์ด๊ธฐ ์ค์
configureInitialSettings()
}
func setupUI() {
// UI ์์ ์ด๊ธฐ ์ค์
view.backgroundColor = .white
navigationItem.title = "My View Controller"
}
func loadData() {
// ์ด๊ธฐ ๋ฐ์ดํฐ ๋ก๋
print("Data loaded")
}
func configureInitialSettings() {
// ๊ธฐํ ์ด๊ธฐ ์ค์
print("Initial settings configured")
}
viewWillappear
VIewController์ view๊ฐ View ๊ณ์ธต์ ์ถ๊ฐ๋๊ธฐ ์ง์ ์ ํธ์ถ
๋ทฐ๊ฐ ํ๋ฉด์ ๋ํ๋ ๋๋ง๋ค ๊ณ์ ํธ์ถ
์ฑ์ ์ด๊ธฐํ ์์ ์ viewDidLoad์์๋ ๋์ง๋ง
๋ค๋ฅธ ๋ทฐ์ ๊ฐ๋ค๊ฐ ๋ค์ ๋์์ค๋ ์ํฉ์์ ํด์ฃผ๊ณ ์ถ์ ์ฒ๋ฆฌ๊ฐ ์๋ค๋ฉด
viewWillAppear์์ ํด์ฃผ๋ฉด ๋๋ค.
- ๋ฐ์ดํฐ ๊ฐฑ์ : ํ๋ฉด์ ๋ํ๋๊ธฐ ์ง์ ์ ์ต์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์์ ๋ทฐ๋ฅผ ์ ๋ฐ์ดํธํ๋ ์์ .
- UI ์ ๋ฐ์ดํธ: ๋ทฐ๊ฐ ๋ํ๋๊ธฐ ์ ์ ์ฌ์ฉ์ ์ธํฐํ์ด์ค๋ฅผ ์ ๋ฐ์ดํธํ๊ฑฐ๋ ๊ตฌ์ฑ ์์๋ฅผ ์ด๊ธฐํํ๋ ์์ .
- ์ํ ์ ์ฅ: ํ๋ฉด์ ๋ค์ ๋ํ๋๊ธฐ ์ ์ ํ์ฌ ์ํ๋ฅผ ์ ์ฅํ๊ฑฐ๋ ๋ณต์ํ๋ ์์ .
- ๋คํธ์ํฌ ์์ฒญ ์ค๋น: ํ๋ฉด์ ๋ํ๋๊ธฐ ์ ์ ํ์ํ ๋คํธ์ํฌ ์์ฒญ์ ์ค๋นํ๋ ์์
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// ์ต์ ๋ฐ์ดํฐ ๋ก๋
updateData()
// UI ์
๋ฐ์ดํธ
refreshUI()
}
func updateData() {
// ๋ทฐ๊ฐ ํ๋ฉด์ ๋ํ๋๊ธฐ ์ ์ ์ต์ ๋ฐ์ดํฐ๋ก ์
๋ฐ์ดํธ
print("Data updated")
}
func refreshUI() {
// ๋ทฐ๊ฐ ํ๋ฉด์ ๋ํ๋๊ธฐ ์ ์ UI๋ฅผ ์
๋ฐ์ดํธ
print("UI refreshed")
}
viewDidAppear
๋ทฐ๊ฐ ํ๋ฉด์ ์์ ํ ๋ํ๋ ์งํ ํธ์ถ๋๋ค.
ViewController๊ฐ View ๊ณ์ธต ๊ตฌ์กฐ์ ์ถ๊ฐ๋ ์งํ
๋ทฐ๊ฐ ๋ํ๋ฌ๋ค๋ ๊ฒ์ ์ปจํธ๋กค๋ฌ์๊ฒ ์๋ฆฌ๋ ์ญํ
ํ๋ฉด์ ์ ์ฉ๋ ์ ๋๋ฉ์ด์ ์ ๊ทธ๋ ค์ค๋ค. ์ด๋ฅผ ์ ์ธํ๊ณ ๋ viewDidAppear์ viewWillAppear๋ ๊ฑฐ์ ๊ฐ๋ค.
์ ๋๋ฉ์ด์ ์์, ๋คํธ์ํฌ ์์ฒญ ์์ ๋ฑ ๋ทฐ๊ฐ ํ๋ฉด์ ๋ํ๋ ํ์ ํ ์์ ์ ์ํํ๋ค.
๋ทฐ๊ฐ ํ๋ฉด์ ๋ํ๋ ํ์ ์์ํ ์์ ์ ์ํํ๋๋ฐ ์ฌ์ฉ๋๋ค.
- ์ ๋๋ฉ์ด์ ์์: ํ๋ฉด์ ์์ ํ ๋ํ๋ ํ์ ์ ๋๋ฉ์ด์ ์ ์์ํ๋ ์์ .
- ๋ฐ์ดํฐ ๋ก๋ ์๋ฃ: ๋ทฐ๊ฐ ์์ ํ ๋ํ๋ ํ์ ์ถ๊ฐ์ ์ธ ๋ฐ์ดํฐ๋ฅผ ๋ก๋ํ๊ฑฐ๋ ์ฒ๋ฆฌ๋ฅผ ์๋ฃํ๋ ์์ .
- ์ฌ์ฉ์ ์ธํฐ๋์ : ์ฌ์ฉ์ ์ธํฐ๋์ ์ ํ์ฑํํ๊ฑฐ๋ ํน์ ์์ ์ ์์ํ๋ ์์ .
- ๋คํธ์ํฌ ์์ฒญ ์์: ํ๋ฉด์ ๋ํ๋ ํ์ ๋น๋๊ธฐ ๋คํธ์ํฌ ์์ฒญ์ ์์ํ๋ ์์ .
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// ์ ๋๋ฉ์ด์
์์
startAnimations()
// ์ฌ์ฉ์ ์ธํฐ๋์
ํ์ฑํ
enableUserInteraction()
}
func startAnimations() {
// ๋ทฐ๊ฐ ํ๋ฉด์ ์์ ํ ๋ํ๋ ํ ์ ๋๋ฉ์ด์
์์
UIView.animate(withDuration: 0.5) {
self.view.alpha = 1.0
}
}
func enableUserInteraction() {
// ๋ทฐ๊ฐ ํ๋ฉด์ ์์ ํ ๋ํ๋ ํ ์ฌ์ฉ์ ์ธํฐ๋์
ํ์ฑํ
print("User interaction enabled")
}
ใ
viewWillDisappear
์ค์ ์คํฌ๋ฆฐ์ ๋ทฐ๊ฐ ์ฌ๋ผ์ง๊ธฐ ์ ์ ํธ์ถ
์ฑ์ ์ฌ์ฉํ๋ค๊ฐ ๋ค๋ฅธ ํ๋ฉด์ผ๋ก ๋์ด๊ฐ๊ฑฐ๋ ์ด์ ํ๋ฉด์ผ๋ก ๋์๊ฐ ๋ ํ์ฌ ํ๋ฉด์ ์ฌ๋ผ์ง๊ธฐ ์ง์ ํธ์ถ
ViewController๊ฐ View ๊ณ์ธต ๊ตฌ์กฐ์์ ์ฌ๋ผ์ง๊ธฐ ์ง์
๋ทฐ๊ฐ ์ญ์ ๋๋ ค๊ณ ํ๋ ๊ฒ์ ๋ทฐ ์ปจํธ๋กค๋ฌ์๊ฒ ์๋ ค์ค๋ค.
๋ทฐ๊ฐ ์ฌ๋ผ์ง๊ธฐ ์ ์ ๋ฐ์ดํฐ ์ ์ฅ, ์ ๋๋ฉ์ด์ ์ค์ง, ๋คํธ์ํฌ ์์ฒญ ์ทจ์ ๋ฑ์ ์์ ์ ์ํํ๋ค.
- ํ์ฌ ์ํ ์ ์ฅ: ๋ทฐ๊ฐ ์ฌ๋ผ์ง๊ธฐ ์ ์ ํ์ฌ ์ํ๋ฅผ ์ ์ฅํ์ฌ ๋ค์์ ๋ทฐ๊ฐ ๋ํ๋ ๋ ๋ณต์ํ ์ ์๊ฒ ํฉ๋๋ค.
- ์ ๋๋ฉ์ด์ ์ค์ง: ๋ทฐ๊ฐ ์ฌ๋ผ์ง๊ธฐ ์ ์ ์คํ ์ค์ธ ์ ๋๋ฉ์ด์ ์ ์ค์งํ๊ฑฐ๋ ์๋ฃํฉ๋๋ค.
- ๋คํธ์ํฌ ์์ฒญ ์ทจ์: ์งํ ์ค์ธ ๋คํธ์ํฌ ์์ฒญ์ด ์๋ค๋ฉด ์ด๋ฅผ ์ทจ์ํฉ๋๋ค.
- ์ฌ์ฉ์ ์ ๋ ฅ ๋นํ์ฑํ: ์ฌ์ฉ์์ ์ ๋ ฅ์ ๋นํ์ฑํํ์ฌ ๋ทฐ๊ฐ ์ฌ๋ผ์ง๋ ๋์ ๋ฐ์ํ ์ ์๋ ์ค๋ฅ๋ฅผ ๋ฐฉ์งํฉ๋๋ค.
- ๋ฐ์ดํฐ ์ ์ฅ: ๋ทฐ๊ฐ ์ฌ๋ผ์ง๊ธฐ ์ ์ ๋ฐ์ดํฐ๋ฅผ ์๊ตฌ ์ ์ฅ์์ ์ ์ฅํฉ๋๋ค.
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// ํ์ฌ ์ํ ์ ์ฅ
saveCurrentState()
// ์ ๋๋ฉ์ด์
์ค์ง
someView.layer.removeAllAnimations()
// ๋คํธ์ํฌ ์์ฒญ ์ทจ์
networkRequest?.cancel()
}
viewDidDisappear
๋ทฐ๊ฐ ๋ทฐ ๊ณ์ธต์์ ์์ ํ ์ฌ๋ผ์ง ํ ํธ์ถ๋๋ค.
ViewController๊ฐ View ๊ณ์ธต๊ตฌ์กฐ์์ ์ฌ๋ผ์ง ์งํ
๋ทฐ๊ฐ ์ฌ๋ผ์ง ํ์ ํด์ผ ํ ์ ๋ฆฌ ์์ ์ ์ํํ๋ค.
- ๋ฆฌ์์ค ํด์ : ๋ทฐ๊ฐ ํ๋ฉด์์ ์์ ํ ์ฌ๋ผ์ง ํ์ ๋ฉ๋ชจ๋ฆฌ ํด์ , ํ์ด๋จธ ๋ฌดํจํ, ์ต์ ๋ฒ ์ ๊ฑฐ ๋ฑ์ ์์ ์ ์ํํฉ๋๋ค.
- UI ์ ๋ฐ์ดํธ ์ ๋ฆฌ: ๋ทฐ๊ฐ ์ฌ๋ผ์ง ํ์ ๋ถํ์ํ UI ์ ๋ฐ์ดํธ๋ฅผ ์ ๋ฆฌํฉ๋๋ค.
- ์ํ ๋ณต๊ตฌ: ๋ทฐ๊ฐ ์ฌ๋ผ์ง ํ์ ๋ค๋ฅธ ์ํ๋ก ๋ณต๊ตฌํด์ผ ํ ํ์๊ฐ ์์ ๋ ์ฌ์ฉํฉ๋๋ค.
- ์ฌ์ฉ์ ์ธํฐ๋์ ๋ณต๊ตฌ: ๋ทฐ๊ฐ ์์ ํ ์ฌ๋ผ์ง ํ์ ๋ค๋ฅธ ๋ทฐ ์ปจํธ๋กค๋ฌ์์ ์ธํฐ๋์ ์ ๋ณต๊ตฌํฉ๋๋ค.
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
// ๋ฆฌ์์ค ํด์
releaseResources()
// ํ์ด๋จธ ๋ฌดํจํ
timer?.invalidate()
timer = nil
// ์ต์ ๋ฒ ์ ๊ฑฐ
NotificationCenter.default.removeObserver(self)
}
์ฐธ๊ณ ๋ ํผ๋ฐ์ค
https://trumanfromkorea.tistory.com/60
https://icksw.tistory.com/187
https://zeddios.tistory.com/43
https://bicycleforthemind.tistory.com/33
'๐ iOS > UIKit' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ ์ด๋ธ๋ทฐ ์ ์ ์ฌ์ฌ์ฉ ๋จ - ๋ธ๋ก๊ทธ ์ถ๊ฐ ์์ฑํ๊ธฐ (0) | 2024.07.28 |
---|---|
textField์ UIKeyboardType (0) | 2024.07.08 |
[iOS] toast message - ๋ฐฐ๋ฌ์ ๋ฏผ์กฑ ์ฅ๋ฐ๊ตฌ๋์ ์ถ๊ฐ๋์ต๋๋ค (0) | 2024.07.07 |
[iOS] Lottie ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น๋ฐฉ๋ฒ - ๋ฐ์น์คํฌ๋ฆฐ ๋ง๋ค๊ธฐ_์ฝ๋๋ฒ ์ด์ค (0) | 2024.07.05 |
[UIKit] UITableView ์ฝ๋๋ฒ ์ด์ค ์ฌ์ฉ๋ฒ (0) | 2024.07.04 |