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

[Swift] ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ํŒจํ„ด iOS

by ํ‹ด๋”” 2021. 3. 24.
๋ฐ˜์‘ํ˜•

A๋ผ๋Š” ๋ทฐ ์ปจํŠธ๋กค์—์„œ B๋ผ๋Š” ๋ทฐ ์ปจํŠธ๋กค๋กœ ์ด๋™ํ•  ๋•Œ ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ฒจ์ค˜์•ผ ํ•  ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿด ๋•Œ ์“ธ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์Šคํ† ๋ฆฌ๋ณด๋“œ๋ผ๋ฉด segue๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ B์— ํ”„๋กœํผํ‹ฐ๋ฅผ ๋งŒ๋“ค์–ด ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ์— A <- B ์ฒ˜๋Ÿผ ํ˜„์žฌ ๋ณด์ด๋Š” ํ™”๋ฉด์ด ์‚ฌ๋ผ์ง€๊ณ  ๋ทฐ๋ฅผ ๋ณด์—ฌ์ง€๊ฒŒ ํ–ˆ๋˜ ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋กœ ๋‹ค์‹œ ์ „๋‹ฌํ•ด์•ผ ํ•  ๋•Œ delegate ํŒจํ„ด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Backward passing data ํ•  ์ˆ˜ ์žˆ๋„๋ก view๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ํŒจํ„ด์„ ์œ„ํ•ด ํ”„๋กœํ† ์ฝœ ์ƒ์„ฑ๊ณผ ์ฑ„ํƒ๊นŒ์ง€ ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ ๋‹ค๋ค„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค~


๋Œ€๋ฆฌ์ž๋Š” ์œ„์ž„์ž๋ผ๊ณ ๋„ ํ•˜๋ฉฐ ์œ„์ž„์ž์—์„œ ์ˆ˜์‹ ์ž์— ์ •์˜๋œ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ์œ„์ž„์ž์—๊ฒŒ ์ž์‹ ์ด ํ•  ์ผ์„ ์œ„์ž„ํ–ˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค(๋ผ๊ณ  ๋งํ•˜์ง€๋งŒ ์ฒ˜์Œ์—” ์ •๋ง ๋‚ฎ์„  ๊ฐœ๋…์ด๋ผ ํž˜๋“ค์—ˆ๋‹ค...)

์ž ์ด ๊ทธ๋ฆผ๋งŒ ๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค ใ…Žใ…Ž

์ด๋ฒˆ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ViewController์™€ BViewController ๋ผ๋Š” ๋‘๊ฐœ์˜ ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ์žˆ๊ณ 

 ViewController ์—์„œ BViewController ๋ฅผ ๋„์›Œ ์ค€๋’ค

BViewController ์—์„œ ์ž…๋ ฅํ•œ ๊ฐ’์„ ViewController ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ ์ž…๋‹ˆ๋‹ค.

๋ง ๊ทธ๋Œ€๋กœ ๋’ค๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ!@

 

์ด๋•Œ ํ•„์š”ํ•œ ๊ฒƒ์€ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ํŒจํ„ด์ž…๋‹ˆ๋‹ค! 

๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ํŒจํ„ด์„ ์œ„ํ•ด 

1. ํ”„๋กœํ† ์ฝœ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค 

ํ”„๋กœํ† ์ฝœ์€ ์ž๋ฐ”์—์„œ implementation๊ณผ ๋น„์Šทํ•˜๊ฒŒ ๊ตฌํ˜„ํ•ด ์ค˜์•ผ ํ•˜๋Š” ํ˜•์‹์„ ์ง€์ •ํ•˜๋Š” ๋ฉ”๋‰ด์–ผ ๊ฐ™์€ ๊ฒƒ์ด๋ผ๊ณ  ๋ณด์‹œ๋ฉด ๋˜์š”

๋”ฐ๋ผ์„œ BViewController๋Š” ํ˜•ํƒœ๋ฅผ ๋งŒ๋“ค๊ณ  ๊ทธ๊ฑธ ViewController ์—์„œ ๊ตฌํ˜„ํ•ด ์ฃผ๋„๋ก ํ•  ๊ฑฐ์—์š”

์ด๋•Œ BViewController ๋Š” delegate๋ผ๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค

2. deleage
์ด ๊ฐ์ฒด์— ViewContrller์—์„œ ์ž์‹ (self) ๋ฅผ delegate ์— ์ง€์ •ํ•ด์ค๋‹ˆ๋‹ค

๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด BViewController ๋Š” delegate ๊ฐ์ฒด๋กœ ViewController์˜ delegate๋ฅผ ์œ„ํ•œ ํ•จ์ˆ˜๋ฅผ  ์‹คํ–‰ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค!!

์ž ๊ทธ๋Ÿฌ๋ฉด ์ด์ œ ์˜ˆ์ œ! 

 


BViewController ์˜ ๋ทฐ ๊ตฌ์„ฑํ•˜๊ธฐ

์šฐ์„  ์Šค์œ„ํ”„ํŠธ ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค๊ณ  ์‹คํ–‰ํ•ด ์ฃผ์„ธ์š”

 ํ”„๋กœ์ ํŠธ ๋งŒ๋“ค๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ์„ธํŒ…๋˜์–ด ์žˆ๋Š” ๊ณณ์—์„œ  ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

 

BViewController.swift ์™€ BViewController.xib๋ฅผ ์ƒ์„ฑํ•ด ์ค๋‹ˆ๋‹ค.

์ด์ œ xib์˜ ๋ทฐ๋ฅผ ์—ฐ๊ฒฐํ•ด ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

BViewControlelr.xib๋ฅผ ๋”๋ธ”ํด๋ฆญ์—์„œ ์ฐฝ์— ๋„์šด๋’ค 

 

BViewController.xib์˜ ์ƒ๋‹จ์— ์žˆ๋Š”

File's Owner ํด๋ฆญ 

๊ฐ€์žฅ ์˜ค๋ฅธ ์ชฝ์— ์žˆ๋Š” connection inspector ํด๋ฆญ

์ €๊ธฐ view - xview O์ด๋ ‡๊ฒŒ ๋˜์–ด ์žˆ๋Š” ๊ณณ์—์„œ O ๋ถ€๋ถ„ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ๋™๊ทธ๋ผ๋ฏธ๋ฅผ ๋“œ๋ž˜๊ทธ ํ•ด์„œ

๊ณ„์ธต ๊ตฌ์กฐ ์ œ์ผ ์ƒ๋‹จ 

View์— ๋–จ๊ถˆ ์ค๋‹ˆ๋‹ค

๊ทธ ํ›„์— 

File's Owner ๋‹ค์‹œ ํด๋ฆญ

Attributes Inspector ์—์„œ ์ปค์Šคํ…€ ํด๋ž˜์Šค๋ฅผ

์ด์–ด์ค„ ํด๋ž˜์Šค ๋ช…์œผ๋กœ ์ง€์ •ํ•ด ์ค๋‹ˆ๋‹ค

 

์ปค๋ฉ˜ํŠธ + ์‰ฌํ”„ํŠธ + L์„ ๋ˆŒ๋Ÿฌ์„œ UILabel๊ณผ textField๋ฅผ view ์— ๋„ฃ์–ด ์ค๋‹ˆ๋‹ค.

    @IBOutlet weak var nameTextField: UITextField!

ํ…์ŠคํŠธ ํ•„๋“œ๋งŒ BViewController ์—์„œ IBOutlet์œผ๋กœ ์ด์–ด์ค๋‹ˆ๋‹ค

BViewController ์— UITextFieldDelegate ๋ฅผ ์ฑ„ํƒํ•ด ์ค๋‹ˆ๋‹ค

class BViewController: UIViewController, UITextFieldDelegate{
// (์ƒ๋žต)
}

์ž ์ด์ œ BViewController ์˜ ๋ทฐ ์ž‘์—…์€ ๋๋‚ฌ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ด ํฌ์ŠคํŒ…์˜ ํ•ต์‹ฌ์ธ 

ํ”„๋กœํ† ์ฝœ์„ ๊ตฌํ˜„ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค~

 


ํ”„๋กœํ† ์ฝœ์„ ๊ตฌํ˜„ํ•ด ์ค๋‹ˆ๋‹ค. 

 

BViewController.swift

protocol BViewControllerDelegate {
    func updateLabel(_ name: String)
}

BViewControllerDelegate๋ฅผ ์ฑ„ํƒํ•˜๋Š” ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋Š” updateLabel(_:) ์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค

์ด์ œ BViewController ์— delegate ํ”„๋กœํผํ‹ฐ๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. 

 var delegate: BViewControllerDelegate?

์ด delegate ํ”„๋กœํผํ‹ฐ๋กœ BViewControllerDelegate๋ฅผ ๊ตฌํ˜„ํ•œ ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ์— ์ •์˜๋œ ํ•จ์ˆ˜์— ์ ‘๊ทผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ํ”„๋กœํ† ์ฝœ์„ ๊ตฌํ˜„ํ–ˆ์œผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋™์ž‘์„ ์œ„ํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ์ค๋‹ˆ๋‹ค.

import UIKit

protocol BViewControllerDelegate {
    func updateLabel(_ name: String)
}

class BViewController: UIViewController, UITextFieldDelegate{
    
    @IBOutlet weak var nameTextField: UITextField!
    
    var delegate: BViewControllerDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let backButton = UIButton()
        backButton.addTarget(self, action: #selector(popViewController), for: .touchUpInside)
        backButton.setImage(UIImage(systemName: "chevron.backward"), for: .normal)
        
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
        
        nameTextField.delegate = self
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        
        if (nameTextField.text != "") {
            delegate?.updateLabel(nameTextField.text ?? "")
            nameTextField.resignFirstResponder()
        }
        
        return true
    }
    
    @objc func popViewController() {
        self.dismiss(animated: true, completion: nil)
    }
}

viewDidLoad ์—์„œ๋Š” ๋„ค๋น„๊ฒŒ์ด์…˜์— ๋“ค์–ด๊ฐˆ ๋ฒ„ํŠผ๊ณผ 

ํ…์ŠคํŠธ ํ•„๋“œ์˜ ๋™์ž‘์„ ์œ„ํ•ด ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๊ฐ์ฒด์— self๋ฅผ ์ง€์ •ํ•ด์ค๋‹ˆ๋‹ค. 

์ด๋ฆ„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด UITextFieldDelegate ๋˜ํ•œ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ํŒจํ„ด! 

 

textFieldShouldReturn(_:) ์€ ํ‚ค๋ณด๋“œ(๋ธ”๋ฃจํˆฌ์Šค ํ‚ค๋ณด๋“œ ํฌํ•จ)์˜ ์—”ํ„ฐ(๋ฆฌํ„ดํ‚ค)๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜ ์ž…๋‹ˆ๋‹ค.

๋งŒ์•ฝ ํ…์ŠคํŠธ ํ•„๋“œ์˜ ํ…์ŠคํŠธ๊ฐ€ ๋นˆ๊ฐ’์ด ์•„๋‹ˆ๋ผ๋ฉด("")

๋ธ๋ฆฌ๊ฒŒ์ดํŠธ๋กœ ์ง€์ •๋œ ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๊ตฌํ˜„๋œ updateLabel(_)์„ ํ˜ธ์ถœํ•ด์„œ ํ…์ŠคํŠธ ํ•„๋“œ์— ์ž…๋ ฅ๋œ ํ…์ŠคํŠธ๋ฅผ ์ „๋‹ฌํ•ด ์ค๋‹ˆ๋‹ค.

๊ทธ ํ›„์— ํ…์ŠคํŠธ ํ•„๋“œ์˜ firstResponder๋ฅผ resign ์‹œ์ผœ ์ค๋‹ˆ๋‹ค.

์ด์ œ BViewControllerDelegate ๋ฅผ ์ฑ„ํƒํ•˜๋Š” ViewController ๋ฅผ ๊ตฌํ˜„ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 


 

Main.stroyboard 

๋ผ๋ฒจ๊ณผ ๋ฒ„ํŠผ์„ ํ•˜๋‚˜์”ฉ ๋ฐฐ์น˜ํ•ด ์ค๋‹ˆ๋‹ค. 

๋ฒ„ํŠผ์€ BViewController ๋ฅผ ํ™”๋ฉด์— Present ํ•ด์ค„ ๋ฒ„ํŠผ์ด๊ณ 

๋ผ๋ฒจ์€ BViewController ์˜ ํ…์ŠจํŠธ ํ•„๋“œ์—์„œ ์ž…๋ ฅ๋œ ํ…์ŠคํŠธ๊ฐ€ ๋ณด์—ฌ์งˆ ๋ผ๋ฒจ์ž…๋‹ˆ๋‹ค. 

 

๊ฐ๊ฐ IBOutlet์„ ์ด์–ด์ฃผ์„ธ์š”

    @IBOutlet weak var nameLabel: UILabel!
    
    @IBOutlet weak var pushViewButton: UIButton!

์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ์ค๋‹ˆ๋‹ค. ์šฐ์„  BViewControllerDelegate๋ฅผ ์ฑ„ํƒํ•ด ์ฃผ์„ธ์š”

class ViewController: UIViewController, BViewControllerDelegate{
}

 

override func viewDidLoad() {
        super.viewDidLoad()
        pushViewButton.addTarget(self, action: #selector(pushBViewcontroller), for:.touchUpInside)
        
    }

๋ฒ„ํŠผ์— addTarget์„ ์ง€์ •ํ•ด ์ค๋‹ˆ๋‹ค. 

pushBViewController() ๋Š” BViewController ๋ฅผ present ํ•˜๋Š”ํ•จ์ˆ˜ ์ž…๋‹ˆ๋‹ค.

    @objc func pushBViewcontroller() {
        let bViewController = BViewController()
        bViewController.delegate = self 
        let navigationController = UINavigationController(rootViewController: bViewController)
        navigationController.modalPresentationStyle = .fullScreen
        
        self.present(navigationController, animated: false, completion: nil)
    }

BViewController์˜ delegate์— self๋กœ ์ž์‹ ์„ ์ง€์ •ํ•ด์ค๋‹ˆ๋‹ค. 

delegate๋กœ ์ด์–ด์ ธ ViewController๋Š” BViewControllerDelegate ์—์„œ ์ง€์ •๋œ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ 

BViewController๋Š” delegate๋ฅผ ํ†ตํ•ด ViewController์—์„œ ๊ตฌํ˜„๋œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

    func updateLabel(_ name: String) {
        nameLabel.text = name
    }

๋งˆ์ง€๋ง‰์œผ๋กœ updateLabel(_:)์„ ๊ตฌํ˜„ํ•ด ์ค๋‹ˆ๋‹ค

 

์ด์ œ ๋นŒ๋“œํ•˜๊ณ  ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ด ์ฃผ๋ฉด

ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์ด์ƒํ•œ ์ ์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค

 

 

 

์ฐธ๊ณ  ์‚ฌ์ดํŠธ:

learnappmaking.com/pass-data-between-view-controllers-swift-how-to/#back-delegation

 

Pass Data Between View Controllers – LearnAppMaking

When your app has multiple User Interfaces, you'll want to move data from one UI to the next. How do you pass data between view controllers in Swift?

learnappmaking.com

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€