๐Ÿง‘โ€๐Ÿณdelegate, ๐ŸŸ๐Ÿžprotocol ๊ฐœ๋…๊ณผ ์ฝ”๋“œ ์˜ˆ์ œ

sangheeยท2021๋…„ 9์›” 2์ผ
0

๐ŸšฉiOS

๋ชฉ๋ก ๋ณด๊ธฐ
14/18
post-thumbnail
post-custom-banner

protocol, delegate

์ฐธ๊ณ ์‚ฌ์ดํŠธ๋ฅผ ์ฐธ๊ณ ํ•˜์˜€๋‹ค.

protocol ๐ŸŸ๐Ÿž

delegate๋Š” protocol๋กœ ๊ตฌํ˜„๋˜๊ธฐ์— protocol์„ ์‚ดํŽด๋ณธ๋‹ค.

protocol์€ ๋ถ•์–ด๋นต ๊ธฐ๊ณ„๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ํŽธํ•œ๋‹ค. ๋ถ•์–ด๋นต ๊ธฐ๊ณ„์—๋Š” ํ‹€, ๋ฐ˜์ฃฝ, ๋ถˆ, ์ตํžˆ๋Š” ํ–‰์œ„ ๋“ฑ์ด ์žˆ๋‹ค. ๋ถ•์–ด๋นต์—๋Š” ํŒฅ, ์Šˆํฌ๋ฆผ, ๊ณ ๊ตฌ๋งˆ ๋“ฑ ์—ฌ๋Ÿฌ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋‹ค. ๋ถ•์–ด๋นต์€ ๋ถ•์–ด๋นต ๊ธฐ๊ณ„๋ฅผ ๋ฌด์กฐ๊ฑด ๊ฐ€์ ธ์•ผ ์™„์„ฑ๋œ๋‹ค.

๋ถ•์–ด๋นต ๊ธฐ๊ณ„๋ฅผ protocol์ด๋ผ๊ณ  ๋ณธ๋‹ค๋ฉด, ๋ถ•์–ด๋นต ๊ธฐ๊ณ„๋ฅผ ์ฑ„ํƒํ•œ ๋ถ•์–ด๋นต๋“ค์€ ํ‹€, ๋ฐ˜์ฃฝ, ๋ถˆ, ์ตํžˆ๋Š” ํ–‰์œ„๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋ฉฐ ๊ฐ๊ฐ ๋‹ค๋ฅธ ํ‹€, ๋ฐ˜์ฃฝ ๋“ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๊ทธ๋ž˜์„œ protocol์—๋Š” ๊ธฐ๋Šฅ์„ ์ „๋ถ€ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์„ ์–ธ๋งŒ ํ•œ๋‹ค.

delegate ๐Ÿง‘โ€๐Ÿณ

delegate๋Š” ์˜์–ด๋กœ ๋Œ€๋ฆฌ์ž๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์•Œ๋ฐ”์ƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. ์•Œ๋ฐ”์ƒ์€ ๋ฉ”๋‰ด์–ผ๋Œ€๋กœ ์ผ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.

์ •๋ฆฌํ•˜์ž๋ฉด, protocol์€ ๋ถ•์–ด๋นต์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ”๋‰ด์–ผ์ด๋ผ๋ฉด, delegate๋Š” ๋ถ•์–ด๋นต์„ ์ƒ์„ฑํ•˜๋Š” ์•„๋ฅด๋ฐ”์ดํŠธ์ƒ์„ ์˜๋ฏธํ•œ๋‹ค.

์ฝ”๋“œ ์˜ˆ์ œ

์ธํ’‹์ฐฝ์„ HomeController์— ์ถ”๊ฐ€ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ™ˆ์—์„œ ์ธํ’‹์ฐฝ์„ ํƒญํ–ˆ์„ ๊ฒฝ์šฐ์—, ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋ ค๊ณ  ํ•œ๋‹ค.

์ฝ”๋“œ์— ๊ฐœ๋… ์ ์šฉ

์ธํ’‹์ฐฝ์€ ๋ถ•์–ด๋นต์ด๊ณ , ๋ทฐ๋Š” ์•Œ๋ฐ”์ƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ํŽธํ•˜๋‹ค.

์ธํ’‹์ฐฝ์—์„œ ํƒญ์ด ๋˜์—ˆ์„ ๋•Œ ์‹คํ–‰๋˜๋Š” presentLocationInputView ํ•จ์ˆ˜๋ฅผ ์„ ์–ธ๋งŒ ํ•˜๊ณ  ์ด ํ•จ์ˆ˜๋ฅผ ๋ทฐ์—์„œ ๊ตฌํ˜„ํ•œ๋‹ค.

์ฝ”๋“œ ์ž‘์„ฑ

1. ์ธํ’‹์ฐฝ์— delegate ์ถ”๊ฐ€

์ธํ’‹์ฐฝ์— protocol๋กœ delegate๋ฅผ ์ •์˜ํ•˜์˜€๋‹ค. ๊ทธ ํ›„ presentLocationInputView ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•˜์˜€๋‹ค.

protocol LocationInputActivationViewDelegate: AnyObject {
    func presentLocationInputView()
}

class LocationInputActivationView: UIView {
    
    weak var delegate: LocationInputActivationViewDelegate?

2. ํƒญ ์ œ์Šค์ณ

์ธํ’‹์ฐฝ์—์„œ ํƒญ ์ œ์Šค์ฒ˜๋ฅผ ์ •์˜ํ•˜์˜€๊ณ , ํƒญ์ด ๋˜์—ˆ์„ ๊ฒฝ์šฐ์—๋Š” presentLocationInputView ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋„๋ก ํ•˜์˜€๋‹ค. ์ด ํ•จ์ˆ˜๋Š” delegate์—์„œ ๊ตฌํ˜„๋œ presentLocationInputViewํ•จ์ˆ˜์ด๋‹ค.

    private func configureTapGesture() {
        let tap = UITapGestureRecognizer(target: self, action: #selector(presentLocationInputView))
        addGestureRecognizer(tap)
    }
    
    @objc func presentLocationInputView() {
        delegate.presentLocationInputView()
    }
}

3. ํ™ˆ

ํ™ˆ์—์„œ ์ธํ’‹๋ทฐ์˜ delegate๋ฅผ ์ž๊ธฐ ์ž์‹ ์œผ๋กœ ์„ค์ •ํ•˜์˜€๊ณ  ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ–ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  presentLocationInputView ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.

class HomeController: UIViewController {

		override func viewDidLoad() {
				configureInputActivationView()
		}
    
    private func configureInputActivationView() {
        inputActivationView.delegate = self
		}
		
		...
}

extension HomeController: LocationInputActivationViewDelegate {
    func presentLocationInputView() {
        print("presentLocationInputView")
    }
}

์ •๋ฆฌ

์ฐธ๊ณ ์‚ฌ์ดํŠธ์—์„œ ์‰ฝ๊ณ  ์นœ์ ˆํ•˜๊ฒŒ ์„ค๋ช…ํ•ด์ฃผ์…”์„œ delegate์— ๋Œ€ํ•ด ์ดํ•ดํ•  ์ˆ˜ ์žˆ์–ด ์ •๋ง ๊ฐ์‚ฌํ–ˆ๋‹ค. ๊ทธ ํ›„ ์‹ค์ œ ๋‚˜์˜ ์ฝ”๋“œ๋กœ ์ ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ๊ณง ๋ถ•์–ด๋นต ์ฒ ์ธ๋ฐ ๋ถ•์–ด๋นต ๋จน๊ณ  ์‹ถ๋‹ค๐Ÿคค

GitHub - Commit

profile
๐Ÿ‘ฉโ€๐Ÿ’ป
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€