Notification Center์ด๋?
ํน์ ์ด๋ฒคํธ ๋ฐ์์,
ํด๋น ์ด๋ฒคํธ์ ๋ฐ์ ์ฌ๋ถ๋ฅผ observe ํ๊ฒ ๋ค๊ณ ๋ฑ๋กํ ๋ชจ๋ observer์๊ฒ ์ ๋ฌํด์ฃผ๋ ๊ฐ์ฒด
์ธ์ ์ฌ์ฉํ๋ฉด ์ข์๊นโ
- ์ผ๋๋ค ๋๋ ๋ค๋๋ค ํต์ ์ด ํ์ํ ๊ฒฝ์ฐ
- ์ฑ ๋ด์์ ๋ช ์์ ์ผ๋ก ์ฐ๊ฒฐ๋์ง ์์ ์ปดํฌ๋ํธ ๊ฐ์ ์ํธ์์ฉ์ด ํ์ํ ๊ฒฝ์ฐ
- ํผ์ด๋ผ๋ ์ด๋ฆ์ notification์ ๊ด์ฐฐํ๊ณ ์๋ FirstVC์๊ฒ ํด๋น noti๊ฐ ์ ๋ฌ๋ฉ๋๋ค!
- SecondVC์์ dismiss completion์ ํด๋น ํผ์ด๋ผ๋ ์ด๋ฆ์ notification ์ post ํด์ฃผ๋ฉด
- FirstVC์์ ํผ์ด๋ผ๋ ์ด๋ฆ์ notification ์ ๊ด์ฐฐํ๊ฒ ๋ค๊ณ addObserve ํ๊ณ
- ex) ํ๋ฉด ์ ํ ํ pop ๋๋ dismissํ ๋ ๋์์จ VC์ ์ํ ๋ฌด์ธ๊ฐ๋ฅผ ์คํํด์ผ ํ๋ ์ํฉ!
Notification Center ์ฌ์ฉ๋ฒ
1๏ธโฃ addObserver
// FirstVC
NotificationCenter.default.addObserver(self, selector: #selector(setToastMessage), name: Notification.Name("showToast"), object: nil)
showToast ๋ผ๋ ์ด๋ฆ์ Notification์ ๊ด์ฐฐํ observer๋ self์ด๊ณ ,
noti๊ฐ ๊ด์ฐฐ๋๋ฉด setToastMessage๋ผ๋ ๋ฉ์๋๋ฅผ ์ํํ๊ฒ ๋ค.
2๏ธโฃ post
// SecondVC
NotificationCenter.default.post(name: Notification.Name("showToast"), object: nil)
showToast๋ผ๋ ์ด๋ฆ์ Notification์ ๋ณด๋ด๊ฒ ๋ค!
Notification Center๋ก ๋ฐ์ดํฐ ์ ๋ฌํ๊ธฐ
์ด ๊ธ์ ์ฐ๊ฒ๋ ๊ฐ์ฅ ํฐ ์ด์ ์ธ๋ฐ ๋๋ฌด ๊ฐ๋จํด์ ๊ทธ๋ฅ ์์ ํ๋๋ง๋ ๋ ๋ถ์ฌ๋ดค์ต๋๋ค..
๋๊ฐ์ง ๋ฐฉ๋ฒ์ด ์์ต๋๋ค!
1๏ธโฃ object
post๋ฅผ ๋ณด๋ผ๋ ์์์ nil๋ก ์ฒ๋ฆฌํด์คฌ๋ object๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋๋ค.
// SecondVC
NotificationCenter.default.post(name: Notification.Name("showToast"), object: "์๋
๐")
postํ ๋ ๋ณด๋ด๊ณ ์ ํ๋ ๊ฐ์ ๋ฃ์ด์ฃผ๊ณ ..
// FirstVC
NotificationCenter.default.addObserver(self, selector: #selector(setToastMessage), name: Notification.Name("showToast"), object: nil)
// ...
@objc
private func setToastMessage(_ notification: Notification) {
...
if let value = notification.object as? String {
print(value) // ์๋
๐
}
}
notification.object ๋ฅผ ํตํด ๊ฐ์ ์ฌ์ฉํ ์ ์์ต๋๋ค!
2๏ธโฃ userInfo
post๋ฅผ ๋ณด๋ผ๋ userInfo์ [AnyHashable : Any] ํํ๋ก ๊ฐ์ ์ ๋ฌํ๋ ๋ฐฉ๋ฒ์ ๋๋ค.
์ฌ๋ฌ key๊ฐ์ ํตํด ์ฌ๋ฌ ๊ฐ์ ์ ๋ฌํ ์ ์์ต๋๋น
// SecondVC
NotificationCenter.default.post(name: Notification.Name("showToast"),
object: "nil",
userInfo: ["userName": "๋น๋ฐ์
๋๋ค", "hungry": true])
[”notificatonKey": value] ํํ๋ก ๋ณด๋ด๊ณ ์ ํ๋ ๊ฐ์ ๋ฃ์ด์ฃผ๊ณ ...
// FirstVC
NotificationCenter.default.addObserver(self, selector: #selector(setToastMessage), name: Notification.Name("showToast"), object: nil)
// ...
@objc
private func setToastMessage(_ notification: Notification) {
...
guard var userName: String = notification.userInfo?["userName"] as? String else { return }
guard let hungry: Bool = notification.userInfo?["hungry"] as? Bool else { return }
print(userName) // ๋น๋ฐ์
๋๋ค
print(hungry) // true
}
notification.userInfo[”notificatonKey”] ์ ํตํด ๊ฐ์ ์ ๊ทผํ ์ ์์ต๋๋ค.
Notification Center๋ฅผ ๋ ์ผ๋ฌด์ง๊ฒ ์ฐ๋ ๋ฒ ๐
extension ๋ถ๋ฆฌ
extension Notification.Name {
static let report = Notification.Name("report")
static let showToast = Notification.Name("showToast")
}
์์ ๊ฐ์ด ๋ฐ๋ก ๋นผ์ ์์๋ก ์ ์ฅํ๋ฉด ๋น๊ต์ ๊ฐ๋จํ๊ณ ํค๊ฐ์ ์คํ๋ฅผ ๋ง๋๋ ๋ฑ์ ์๋ฌ๋ฅผ ๋ง์ ์ ์์ต๋๋ค.. ๐
๊ฐ๋จํ๊ฒ ํ ์คํธํด๋ดค๋ ์ฝ๋.. ๐ญ
GitHub - yangsubinn/Test-iOS: ์ด๊ฒ ์ ๊ฒ ๋ค ํ ์คํธํด๋ณด๋ ๋ ํฌ๐
์ด๊ฒ ์ ๊ฒ ๋ค ํ ์คํธํด๋ณด๋ ๋ ํฌ๐. Contribute to yangsubinn/Test-iOS development by creating an account on GitHub.
github.com
'iOS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[iOS] Compositional Layout ๐โโ๏ธ (0) | 2022.09.05 |
---|---|
[iOS] ์ปดํฌ๋ํธ๋ฅผ ์ฝ๋๋ฒ ์ด์ค, ์คํ ๋ฆฌ๋ณด๋์์ ๋๋ค ์ธ ์ ์๋๋ก ํ๋ ๋ฐฉ๋ฒ๐ฏ (1) | 2022.05.30 |
[iOS] RxDataSource ๐ก (0) | 2022.05.17 |
[iOS] Haptic ์ฌ์ฉ๋ฒ๐ (0) | 2022.04.23 |
[iOS] Diffable Datasourceโ (0) | 2022.03.18 |