Swift - NSNotificationCenter 알림 전송 및 알림 수신
알림(NSNotification) 소개
여기서 말한 통지는 사용자에게 보내는 통지 소식이 아니라 시스템 내부에서 정보를 전달하는 통지를 가리킨다.통지를 소개하기 전에 관찰자 모델이 무엇인지 알아야 한다.
관찰자 모드(Observer): 한 대상이 상태가 변할 때 다른 대상에게 통지하는 것을 말한다.참가자들은 다른 대상이 구체적으로 무엇을 하는지 알 필요가 없다.이것은 결합도를 낮추는 디자인이다.흔히 볼 수 있는 사용 방법은 관찰자가 감청을 등록하고 상태가 바뀔 때 모든 관찰자가 통지를 받는 것이다.
MVC에서 관찰자 모드는 모델 대상과 View 대상이 교류할 수 있도록 허락해야 하며 직접적인 연관이 있을 수 없다는 것을 의미한다.코코아는 두 가지 방식으로 관찰자 모델을 실현했다. 하나는 Key-Value Observing(KVO)이고 다른 하나는 본고에서 말하고자 하는 Notification이다.
관찰자 추가 NSNotificationCenter.defaultCenter().addObserver(self, selector:#selector(TranscriptDetailViewController.getExamId(_:)), name: "examDidChanged", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector:#selector(TranscriptDetailViewController.getExamCourseId(_:)), name: "examCourseDidChanged", object: nil)
func getExamId(notification:NSNotification) {
let userInfo = notification.userInfo as! [String: AnyObject]
let examId = userInfo["examId"] as! String
let examName = userInfo["examName"] as! String
print("\(name) , [\(examId),\(examName)]")
NSUserDefaultsHelper().setDefault("examId", value: examId)
NSUserDefaultsHelper().setDefault("examName", value: examName)
getStudentReport()
}
func getExamCourseId(notification:NSNotification) {
let userInfo = notification.userInfo as! [String: AnyObject]
let examCourseId = userInfo["examCourseId"] as! String
let courseName = userInfo["courseName"] as! String
print("\(name) , [\(examCourseId),\(courseName)]")
NSUserDefaultsHelper().setDefault("examCourseId", value: examCourseId)
getStudentReport()
}
알림 스니퍼 제거
필요하지 않으면 메모리 낭비나 붕괴를 피하기 위해 상응하는 알림을 취소에 등록하는 것을 잊지 마십시오 deinit {
//
// , ,
NSNotificationCenter.defaultCenter().removeObserver(self)
}
알림 보내기 NSNotificationCenter.defaultCenter().postNotificationName("examDidChanged",
object: self, userInfo: ["examId":examInfo.examId,"examName":examInfo.examName])
NSNotificationCenter.defaultCenter().postNotificationName("examCourseDidChanged",
object: self, userInfo: ["examCourseId":examCoureseInfo.examCourseId,"courseName":examCoureseInfo.courseName])
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSON
JSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다.
그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다.
저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.
NSNotificationCenter.defaultCenter().addObserver(self, selector:#selector(TranscriptDetailViewController.getExamId(_:)), name: "examDidChanged", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector:#selector(TranscriptDetailViewController.getExamCourseId(_:)), name: "examCourseDidChanged", object: nil)
func getExamId(notification:NSNotification) {
let userInfo = notification.userInfo as! [String: AnyObject]
let examId = userInfo["examId"] as! String
let examName = userInfo["examName"] as! String
print("\(name) , [\(examId),\(examName)]")
NSUserDefaultsHelper().setDefault("examId", value: examId)
NSUserDefaultsHelper().setDefault("examName", value: examName)
getStudentReport()
}
func getExamCourseId(notification:NSNotification) {
let userInfo = notification.userInfo as! [String: AnyObject]
let examCourseId = userInfo["examCourseId"] as! String
let courseName = userInfo["courseName"] as! String
print("\(name) , [\(examCourseId),\(courseName)]")
NSUserDefaultsHelper().setDefault("examCourseId", value: examCourseId)
getStudentReport()
}
알림 스니퍼 제거
필요하지 않으면 메모리 낭비나 붕괴를 피하기 위해 상응하는 알림을 취소에 등록하는 것을 잊지 마십시오 deinit {
//
// , ,
NSNotificationCenter.defaultCenter().removeObserver(self)
}
알림 보내기 NSNotificationCenter.defaultCenter().postNotificationName("examDidChanged",
object: self, userInfo: ["examId":examInfo.examId,"examName":examInfo.examName])
NSNotificationCenter.defaultCenter().postNotificationName("examCourseDidChanged",
object: self, userInfo: ["examCourseId":examCoureseInfo.examCourseId,"courseName":examCoureseInfo.courseName])
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSON
JSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다.
그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다.
저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.
deinit {
//
// , ,
NSNotificationCenter.defaultCenter().removeObserver(self)
}
NSNotificationCenter.defaultCenter().postNotificationName("examDidChanged",
object: self, userInfo: ["examId":examInfo.examId,"examName":examInfo.examName])
NSNotificationCenter.defaultCenter().postNotificationName("examCourseDidChanged",
object: self, userInfo: ["examCourseId":examCoureseInfo.examCourseId,"courseName":examCoureseInfo.courseName])
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.