Project 05 - NotificationCenter

DaY·2021년 3월 26일
1

iOS

목록 보기
17/52
post-thumbnail

NotificationCenter에 등록된 Event가 발생하면 해당 Event에 대한 행동을 취한다.
등록된 객체들에게 동시에 Notification을 전달해주는 클래스이다.

Notification의 중계자 역할

Post

NotificationCenter.default.post(name: "notificaiton 이름", object: "전달할 값")

post는 전송하는 notification이다.
notification을 Observer에게 값을 전달해준다.

Observer

Observername 키 값을 탐지한다.
어떤 객체가 지정한 notification name으로 notification을 전송하면 observer에서 탐지하여 수행한다.

NotificationCenter.default.addObserver(observer: Any, selector: Selector, name: NSNotification.Name?, object: Any?)
  • observer : observer로 등록할 객체
  • selector : notification을 받았을 때 실행할 동작
  • name : 받고자 하는 notification 이름

RemoveObserver

NotificationCenterSingleton 인스턴스로, 여러 오브젝트를 공유한다.
따라서 등록된 오브젝트가 해제되면 NSNotificationCenter에서도 Observer를 제거했다고 알려야 한다.

// 등록된 Observer 전체 제거
NotificationCenter.removeObserver(self)

// 특정 Observer 제거
NotificationCenter.removeObserver(self, name: NSNotification.Name?, object: Any?)

0개의 댓글