์ƒˆ์†Œ์‹

๐Ÿ“ฑ iOS/-- UIKit

(iOS) ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ํŒจํ„ด์„ ํ†ตํ•ด ์ด์ „ ํ™”๋ฉด์— ๊ฐ’ ์ „๋‹ฌํ•˜๊ธฐ (Delegate , Protocol)

  • -

Delegate ํŒจํ„ด์œผ๋กœ ์ด์ „ํ™”๋ฉด์— ๊ฐ’ ์ „๋‹ฌํ•˜๊ธฐ

์ด์ „ ํ™”๋ฉด์œผ๋กœ ๊ฐ’์„ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ๊ทธ ์ค‘ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด์ „ ํ™”๋ฉด์œผ๋กœ ๊ฐ’์„ ๋„˜๊ธฐ๋Š” ๋ถ€๋ถ„์„ ๊ณต๋ถ€ํ–ˆ๋‹ค.

์ตœ๋Œ€ํ•œ ๊ฐ„๋‹จํ•˜๊ฒŒ ์˜ˆ์ œ๋ฅผ ๋งŒ๋“ค์–ด๋ณด์•˜๋‹ค. ๋ฐ์ดํ„ฐ ์ „๋‹ฌ ์ด์™ธ์˜ ๋ถ€๋ถ„์€ ์„ค๋ช…์ƒ๋žต...

๋™์ž‘ํ™”๋ฉด

File ๊ตฌ์„ฑ

  • BeforeVC.swift
  • AfterVC.swift
  • ProtocolDelegate

์ด๋ ‡๊ฒŒ ๋‘๊ฐœ์˜ Scene๊ณผ VeiwController ๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.

Button, Textfiled, Label๋“ฑ์„ ๊ตฌ์„ฑํ•ด์ฃผ๊ณ , IBOultet์„ ์—ฐ๊ฒฐํ•ด์ค€๋‹ค.

1. ํ”„๋กœํ† ์ฝœ ์ •์˜ํ•˜๊ธฐ

์ƒˆ๋กœ์šด ํŒŒ์ผ์„ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ”„๋กœํ† ์ฝœ์„ ์ •์˜ํ•˜๋Š”๋ฐ, ํ”„๋กœํ† ์ฝœ์€ ๋ฉ”์†Œ๋“œ์˜ ํ”„๋กœํ† ํƒ€์ž…๋งŒ ์ ๊ณ  ๋‚ด๋ถ€ ๋™์ž‘์ฝ”๋“œ๋Š” ์ ์ง€ ์•Š๋Š”๋‹ค.

DelegateProtocol.swift

import UIKit
protocol SendData {
    func send(_ vc: UIViewController, Input value : String?)
}

2. AfterVC์—์„œ ํ”„๋กœํ† ์ฝœ ํƒ€์ž…์˜ ๋ณ€์ˆ˜ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ

AfterVC.swift

import UIKit

class AfterViewController: UIViewController {
    var delegate : SendData? // SendData ํƒ€์ž…์˜ ๋ณ€์ˆ˜์ƒ์„ฑ
    @IBOutlet weak var inputField: UITextField!
    @IBAction func ChangeBtn(_ sender: Any) {
        delegate?.send(self, Input: inputField.text)
      // SendData ํ”„๋กœํ† ์ฝœ์— ์žˆ๋Š” send๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ
        self.presentingViewController?.dismiss(animated: true, completion: nil)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

3. BeforeVC์—์„œ ํ”„๋กœํ† ์ฝœ ์ฑ„ํƒํ•˜๊ณ  ๋ฉ”์†Œ๋“œ ๊ตฌํ˜„ํ•˜๊ธฐ

BeforeVC.swift

import UIKit
class BeforeViewController: UIViewController {
    @IBOutlet weak var valueLabel: UILabel!
    @IBAction func valueChangeBtn(_ sender: Any) {
        guard let AfterVC = self.storyboard?.instantiateViewController(withIdentifier: "AfterVC") as? AfterViewController else{return}

        AfterVC.delegate = self // AfterVC์—์„œ ์ƒ์„ฑํ•œ delegate๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด์„œ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ์ง€์ •.
        AfterVC.modalPresentationStyle = .fullScreen
        present(AfterVC, animated: true, completion: nil)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}
extension BeforeViewController: SendData {
    func send(_ vc: UIViewController, Input value: String?) {
        valueLabel.text = value
    }
}

 

2 ์—์„œ AfterVC์— delegate๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํ”„๋กœํ† ์ฝœ์˜ ํƒ€์ž…์œผ๋กœ ์„ ์–ธํ–ˆ๋‹ค. ๊ทธ ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด์„œ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ๋ฅผ ์ง€์ •ํ•˜๊ฒŒ ๋œ๋‹ค.AfterVC.deleaget = self ์ฆ‰, AfterVC์˜ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ๋Š” BeforeVC(self)๊ฐ€ ๋œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ํ”„๋กœํ† ์ฝœ์„ ์ •์˜ํ• ๋•Œ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•˜๊ธฐ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์„œ ๊ตฌํ˜„ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

extension BeforeViewController: SendData {
    func send(_ vc: UIViewController, Input value: String?) {
        valueLabel.text = value
    }
}

 

 

 

์†Œ์Šค์ฝ”๋“œ

 

https://github.com/Youjunyong/ios_practice.git

 

GitHub - Youjunyong/ios_practice

Contribute to Youjunyong/ios_practice development by creating an account on GitHub.

github.com

 

#2๋ฒˆ ํด๋”์˜ ํ”„๋กœ์ ํŠธ์— ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค.

์‚ฌ์šฉ์ž์—๊ฒŒ ์ž…๋ ฅ๊ฐ’์„ ๋ฐ›์•„, dismissํ•˜๋ฉด ์ด์ „ View์—์„œ ๊ทธ ๊ฐ’์„ ๋„์›Œ์ค€๋‹ค

์—ฐ์Šต ๋ชฉ์ 

  1. DelegateํŒจํ„ด์„ ํ†ตํ•˜์—ฌ ์ด์ „ํ™”๋ฉด์œผ๋กœ ๊ฐ’ ์ „๋‹ฌ ํ•˜๊ธฐ.
  2. Protocol ์„ ์ง์ ‘ ์ž‘์„ฑํ•˜๊ณ , ์ด์ „ ViewController๋ฅผ ๋‹ค์Œ ViewController์˜ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ๋กœ ์ง€์ •ํ•˜๋Š” ์—ฐ์Šต.
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.