iOS - 실무관련/iOS
NotificationCenter로 다른 VC 조종하기
kkkkk1023
2022. 8. 17. 01:59
NotificationCenter란?
NotificationCenter 에 등록된 event 가 발생하면 해당 event에 대한 행동을 취합니다.
앱 내에서 메세지를 던지면 아무데서나 이 메세지를 받을 수 있게 하는 역할을 합니다.
보통 백그라운드 작업의 결과, 비동기 작업의 결과 등 현재 작업의 흐름과 다른 흐름의 작업으로부터 이벤트를 받을 때 사용합니다.
NotificationCenter으로 다른 VC 조종하기
1. 데이터를 보내는 VC (본인이 원하는 타이밍에)
NotificationCenter.default.post(name: Notification.Name(rawValue: "지정할 이름"), object: nil)
2. 데이터를 받는 VC
// 옵저버 등록
NotificationCenter.default.addObserver(self, selector: #selector(함수이름(_:)), name: Notification.Name(rawValue: "지정할 이름"), object: nil)
@objc func 함수이름(_ notification: Notification) {
print("Test Notification")
}