๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS

[Swift] Notification์œผ๋กœ view์™€ view ๋ฐ์ดํ„ฐ ์ ˆ๋‹ฌํ•˜๊ธฐ

by ํ‹ด๋”” 2020. 10. 7.
728x90
๋ฐ˜์‘ํ˜•

๋‚˜์ค‘์— ์ฐธ๊ณ  ํ•˜๋ ค๊ณ  ์ ๋Š” ๊ธ€

 

ํด๋ž˜์Šค ์‹ ์›์„ ์ฒดํฌํ•  ์ˆ˜ ์—†๊ฒŒ ๋˜์„œ(?) 

๋“œ๋””์–ด

Notification์„ ์“ฐ๊ธฐ๋กœ ํ–ˆ๋‹ค!

์™œ RxRxํ•˜๋Š”์ง€ ์•Œ๊ฑฐ ๊ฐ™๋‹ค... ใ…Žใ…Žใ…Ž

 

1. Notification ๋“ฑ๋กํ•˜๊ธฐ

        NotificationCenter.default.addObserver(self,
                                               selector: #selector(updateDateLabel(_:)),
                                               name: .updateYearDate,
                                               object: nil)
        
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(updateHour(_:)),
                                               name: .updateHour,
                                               object: nil)
        
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(updateMin(_:)),
                                               name: .updateMin,
                                               object: nil)

์šฐ์„  ๋‹ค๋ฅธ ๊ณณ์—์„œ ๊ฐ’์„ ๋ฐ›์•„์™€์„œ ์ž‘์—…ํ•ด ์ค„ ๋ทฐ์— ๋“ฑ๋กํ•ด ์ฃผ์ž! viewDidLoad์—๋‹ค๊ฐ€ ์ •์˜ํ•ด ์ฃผ๋ฉด ๋œ๋‹ค.

์ด๋•Œ name์—๋‹ค๊ฐ€ ๊ฐ ๋…ธํ‹ฐํ”ผ์ผ€์ด์…˜ ์ด๋ฆ„์„ ์ ์–ด ์ฃผ์–ด์•ผ ํ•˜๋Š”๋ฐ ์›๋ž˜๋Š” Notification.Name("๋…ธํ‹ฐํ”ผ์ผ€์ด์…˜์ด๋ฆ„") ์ด๋Ÿฐ ์‹์œผ๋กœ ๋“ค์–ด๊ฐ„๋‹ค.

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋…ธํ‹ฐํ”ผ์ผ€์ด์…˜์„ ๋ณด๋‚ด๋Š” ์ชฝ์—๋„ ์ด๋ ‡๊ฒŒ ๋“ค์–ด๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— ๋ณด๊ธฐ๋„ ์–ด๋ ต๊ณ  ์ด๋ฆ„์„ ๋ฐ”๊ฟ€๋ ค๋ฉด ๋“ฑ๋กํ•˜๋Š” ๊ณณ ๋ณด๋‚ด๋Š” ๊ณณ ๋‘˜ ๋‹ค ๋ฐ”๊ฟ” ์ค˜์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ถ”ํ›„ ๊ด€๋ฆฌ๋„ ์–ด๋ ต๊ฒŒ ๋œ๋‹ค. ๊ทธ๋ž˜์„œ NSNotification.Name ์„ extention์œผ๋กœ ๋นผ์ฃผ๋ฉด ๋œ๋‹ค!

 

TA DA~

extension NSNotification.Name {
    static let updateYearDate = Notification.Name("updateYearDate")
    static let updateHour = Notification.Name("updateHour")
    static let updateMin = Notification.Name("updateMin")
}

 

2. ๋ฉ”์†Œ๋“œ ๊ตฌํ˜„

Objective C์˜ ํ”์ ์„ ๊ฐ€์ง„ ๋ฉ”์†Œ๋“œ

dictionaryํƒ€์ž…์˜ userInfo๋กœ ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.

extension ViewController {
    @objc func updateDateLabel(_ notification: NSNotification) {
        if let txt = notification.userInfo?["date"] as? String {
            self.lblYear.text = "\(txt)  "
        }
    }
    
    @objc func updateHour(_ notification: NSNotification) {
        if let txt = notification.userInfo?["hour"] as? String {
            self.lblHour.text = "\(txt)  "
        }
    }
    
    @objc func updateMin(_ notification: NSNotification) {
        if let txt = notification.userInfo?["min"] as? String {
            self.lblMin.text = "\(txt)  "
        }
    }
}

 

3. notification post

์ด์ œ ๋…ธํ‹ฐํ”ผ์ผ€์ด์…˜ post๋ฅผ ์ž‘์„ฑํ•˜์ž!

NotificationCenter.default.post(name: .updateYearDate, object: nil, userInfo: dateTxt)

์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•ด ์ฃผ๋ฉด ๋œ๋‹ค

 

๊ฐ€๋”์€ ์‚ถ์ด ์ฐธ ์‹ ๊ธฐํ•˜๋‹ค

1๋…„ ์ „๋งŒ ํ•ด๋„ ํ•™๊ณผ ์• ํ”Œ ๋งค์ง ๋งˆ์šฐ์Šค ๋งŒ์ง€๋ฉด์„œ ๋ง‰ ์†์— ๋•€๋‚˜๊ณ  ๊ทธ๋žฌ๋Š”๋ฐ ใ…‹ใ…‹ใ…‹ใ…‹

๊ทธ ๋‹น์‹œ ์‹ญ๋งŒ์›์ด๋ผ ์กฐ์‹ฌํžˆ ์จ์•ผ ๋œ๋‹ค๋Š” ๋‹น๋ถ€ ๋“ค์œผ๋ฉด์„œ ์กฐ๊ธˆ ๋ฌด์„œ์› ๋‹ค..

๊ทผ๋ฐ ์ง€๊ธˆ์€ ์—ฐ์žฅ์ด๋ผ๊ณ  ํ•ด์•ผ ํ•˜๋‚˜ 

๋„ˆ๋ฌด ํŽธํ•˜๊ณ  ์ข‹์Œ :)

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€