나만의 공부 노트

Notification Center에 관하여 본문

정리 X/Notification

Notification Center에 관하여

va95 2021. 2. 6. 02:05

대표적으로 키보드가 나타나는 걸 알려주는 기능이 있음

 

(키보드는 따로 UI가 없다네,, 불렀던 view로 키보드 설정을 바꿀수 있음

여기선 UITextInputTraits protocol로 값들을 바꿀 수 있음)

 

 


 

 

 

 

나중에 Notification을 구현하고 추가할 수 있는 듯? 일단 패쓰

 

 

jinshine.github.io/2018/07/05/iOS/NotificationCenter/ <- 설명 잘 되어있다!

 


 

NotificationCenter.default <- 싱글턴 패턴

 

알림을 보낼 땐:

NotificationCenter.default.post(name:"myNoti", object:" 전달할 값")

 

옵저버 등록:

NotificationCenter.default.addObserver(observer:Any, selector:Selector, name: NSNotification.Name?, object:Any?)

 

옵저버 제거:

notiCenter.removeObserver(self)

notiCenter.removeObserver(self, name: NSNotification.Name.UIKeyboardDidShow, object: nil)

 

*보통은 Counterpart되는곳에 맞춰서 해제 시켜줍니다.

viewWillAppear < - > viewWillDisappear
viewdidload() < - > deinit() : 1번씩만 호출되니까

 

 


아래는 예제

 

 

 

키보드가 나타나면 UI 위치들을 변경해야할 때가 있음

(text field가 가려지는 등..)

 

그래서 NSNotificationCenter을 이용하자!

UIKeyboard{will,Did,Show,Hide}Notifications <- 미리 name에 정의되어 있는듯(<#T##NSNotification.Name?#> <- enum 같음)

 

NotificationCenter.default.addObserver(self, selector: <#T##Selector#>, name: UIKeyboardDidShowNotification, object: view.window)

 

 

// selector에 넣어주면 됨

func theKeyBoardAppeared(notification: NSNotification) {

    notification.userInfo <- 상세 정보 담고 있는 변수

}