๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐ŸŽ iOS/iOS Programming

[iOS, Swift] ContainerView ๋งŒ๋“ค๊ธฐ, View๊ฐ„ ๋ฐ์ดํ„ฐ ์ „๋‹ฌํ•˜๊ธฐ - 1. prepare ๋ฉ”์†Œ๋“œ, 2.Children, parent ํ”„๋กœํผํ‹ฐ ์‚ฌ์šฉ

์ปจํ…Œ์ด๋„ˆ ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ(Container View Controller)์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž

Container View

์ฝ˜ํ…์ธ  ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋Š” ์ด๋ฏธ์ง€๋‚˜ ํ…์ŠคํŠธ, HTML ํŽ˜์ด์ง€ ๋“ฑ ์•ฑ์˜ ํ™”๋ฉด์— ํ‘œํ˜„ํ•  ์ฝ˜ํ…์ธ ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ์ž…๋‹ˆ๋‹ค. 

์ด๋ฅผ ์œ„ํ•ด ์ฝ˜ํ…์ธ  ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋Š” ํ™”๋ฉด ์ „์ฒด ์‚ฌ์ด์ฆˆ์˜ ๋ฃจํŠธ ๋ทฐ๋ฅผ ๋‚ด์žฅํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, ์ด ๋ทฐ ์œ„์— ๊ฐ์ข… ์ฝ˜ํ…์ธ ๋ฅผ ์–น์–ด ํ™”๋ฉด์— ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

iOS๋Š” ํ™”๋ฉด์˜ ์ œ์•ฝ์œผ๋กœ ์ธํ•ด ๊ธฐ๋ณธ์ ์œผ๋กœ ํ•œ ํ™”๋ฉด์— ํ•˜๋‚˜์˜ ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋งŒ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋‚˜๋จธ์ง€ ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ์— ์ ‘๊ทผํ•˜๋Š” ํšจ์œจ์ ์ธ ์ˆ˜๋‹จ์„ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.

์ด๋ฅผ ์œ„ํ•ด ์ปจํ…Œ์ด๋„ˆ ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋Š” ํ•˜๋‚˜์˜ ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๋‹ค๋ฅธ ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์‰ฝ๊ณ  ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์ด์–ด์ง€๋„๋ก ๋‹ค์–‘ํ•œ ์ˆ˜๋‹จ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

 

Apple Developer Documentation

 

developer.apple.com


๐Ÿ”ฅ Container View ๋งŒ๋“ค๊ธฐ

 

Container View๋ฅผ ๋งŒ๋“œ๋Š” ๋ฒ•์€ ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค.๐Ÿ˜Š

Container View Object๋ฅผ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

์ถ”๊ฐ€ํ•˜๋ฉด, ์˜ค๋ฅธ์ชฝ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ž๋™์œผ๋กœ ์ž‘์€ ๋ทฐ๊ฐ€ ์ƒ๊น๋‹ˆ๋‹ค.

์ถ”๊ฐ€ํ•œ Container View๋ฅผ ์ปจํŠธ๋กคํ•  VC(ContainerViewController.swift)๋ฅผ ์ƒ์„ฑ ํ›„ ์—ฐ๊ฒฐํ•ด์ค๋‹ˆ๋‹ค.

๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ปฌ๋Ÿฌ๋ฅผ ๋ฐ”๊ฟ”์„œ ํ…Œ์ŠคํŠธํ•ด๋ณธ ํ™”๋ฉด์ž…๋‹ˆ๋‹ค. ์ž˜ ์—ฐ๊ฒฐ๋˜์—ˆ๋„ค์š”.๐Ÿ‘๐Ÿป 


๐Ÿ”ฅ Container View ๋ฐ์ดํ„ฐ ์ „๋‹ฌํ•˜๊ธฐ - 1. prepare ๋ฉ”์†Œ๋“œ  ์‚ฌ์šฉ

1. Segue idenifier ์„ค์ •

segue๋Š” ๋‘ ๋ทฐ์‚ฌ์ด์˜ ๊ด€๊ณ„๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. Storyboar์—์„œ ์—ฐ๊ฒฐํ•  Segue๋ฅผ ์„ ํƒํ•˜์—ฌ Identifier ๊ฐ’์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. 

 

2. prepare ๋ฉ”์†Œ๋“œ ์ •์˜

segue๋ฅผ ํ†ตํ•œ ํ™”๋ฉด ์ „ํ™˜์ด ์ผ์–ด๋‚˜๊ธฐ ์ง์ „, ์‹œ์Šคํ…œ์— ์˜ํ•ด ์ž๋™์œผ๋กœ prepare(for: sender) ๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ์ด ๋ฉ๋‹ˆ๋‹ค.

(์ถœ๋ฐœ์ง€์ธ source / ๋„์ฐฉ์ง€์ธ destination)

prepare ๋ฉ”์†Œ๋“œ์— storyboard์—์„œ ์„ค์ •ํ•œ ๋™์ผํ•œ segue identifier๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์—ฐ๊ฒฐํ•ด์ค๋‹ˆ๋‹ค.

์—ฐ๊ฒฐ ํ›„ sugue์˜ destination ํ”„๋กœํผํ‹ฐ์— ContainerVC ์—ฐ๊ฒฐ ํ›„ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.

// AViewController - ๋ถ€๋ชจ View
Class AViewController : UIViewController {

    //ContainerView ์˜ ViewDidLoad๋ณด๋‹ค ๋จผ์ € ์‹คํ–‰
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    //storyboard์—์„œ ์„ค์ •ํ•œ identifier์™€ ๋™์ผํ•œ ์ด๋ฆ„
        if segue.identifier == "embedContainer" {
            let containerVC = segue.destination as! ContainerViewController
            containerVC.testStr = "Value from Prepare: \(containerValue)"
        }
    }
}

ViewdidLoad์—์„œ ์ „๋‹ฌ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฟŒ๋ ค์ฃผ์–ด ํ™”๋ฉด์— ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

//ContainerViewController - ์ž์‹ View
Class ContainerViewController : UIViewController {
    var testStr: String?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.testLabel.text = testStr
    }
}

 

[๊ฒฐ๊ณผ ํ™”๋ฉด]

 

 

 

 


๐Ÿ”ฅ Container View ๋ฐ์ดํ„ฐ ์ „๋‹ฌํ•˜๊ธฐ - 2. children, parent ํ”„๋กœํผํ‹ฐ ์‚ฌ์šฉ

์ด ๋ฐฉ๋ฒ•์€ ๋ถ€๋ชจ View์™€ ์ž์‹ View ์‚ฌ์ด์˜ ๋ฐ์ดํ„ฐ ์ „์†ก ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

Container View๋ฅผ ์ž์‹ View๋กœ ์ƒ์„ฑํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœํผํ‹ฐ๋งŒ ๋‹ค๋ฅด๊ณ  process๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

 

[process]

1. ๋ฐ์ดํ„ฐ ์ „์†ก ๋ฉ”์„œ๋“œ ์ƒ์„ฑ

2. children, parent ํ”„๋กœํผํ‹ฐ๋กœ ์—ฐ๊ฒฐํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์ „์†ก ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ


1) ๋ถ€๋ชจ View -> ์ž์‹ View

To send data from AViewController to ContainerViewController

: children ํ”„๋กœํผํ‹ฐ ์‚ฌ์šฉ

var children: [UIViewController] { get }

๋ถ€๋ชจ๋ทฐ์— ๋ฒ„ํŠผ ํด๋ฆญ์‹œ ๋ณ€๊ฒฝ๋˜๋Š” ๊ฐ’์„ ์ž์‹๋ทฐ์— ์ „๋‹ฌํ•˜๊ฒŒ ํ…Œ์ŠคํŠธํ•ด๋ณด์•˜์–ด์š”.

 

๋จผ์ €, ์ž์‹๋ทฐ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.

//ContainerViewController - ์ž์‹ View
Class ContainerViewController : UIViewController {
    @IBOutlet weak var testLabel: UILabel!

    func ChangeLabel(labelToChange : Int){
        self.testLabel.text = "Value from Parent : \(labelToChange)"
    }
}

๋ถ€๋ชจ๋ทฐ์—์„œ button ์•ก์…˜์—์„œ children ํ”„๋กœํผํ‹ฐ๋ฅผ ํ†ตํ•ด ์ ‘๊ทผํ•˜์—ฌ ์ž์‹๋ทฐ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋ƒ…๋‹ˆ๋‹ค.

// AViewController - ๋ถ€๋ชจ View
Class AViewController : UIViewController {
    var containerValue: Int = 1
    
    @IBAction func changeContainerValue(_ sender: Any) {
        containerValue += 1
        // childView๋กœ ๊ฐ’ ๋ณด๋ƒ„
        let CVC = children.last as! ContainerViewController
        CVC.ChangeLabel(labelToChange: containerValue)
    }
}

[๊ฒฐ๊ณผ ํ™”๋ฉด]

 


2) ์ž์‹ View ->  ๋ถ€๋ชจ View 

To send data from ContainerViewController -> AViewController

: parent ํ”„๋กœํผํ‹ฐ ์‚ฌ์šฉ

weak var parent: UIViewController? { get }

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋จผ์ €, ๋ถ€๋ชจ๋ทฐ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๋Š” ๋ฉ”์†Œ๋“œ(dataFromContainer)๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.

Class AViewController : UIViewController {
    @IBOutlet weak var messageFromBVCLabel: UILabel!
    
    // childView์—์„œ ๊ฐ’ ๋ฐ›์•„์˜ด
    func dataFromContainer(containerData : String){
        messageFromBVCLabel.text = "Value From Children : \(containerData)"
    }
}

์ž์‹๋ทฐ์—์„œ parent ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ€๋ชจ๋ทฐ์— ์ ‘๊ทผํ•ฉ๋‹ˆ๋‹ค.

์ ‘๊ทผ ํ›„, ๊ตฌํ˜„ํ•œ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐ”๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค.

Class ContainerViewController : UIViewController {
    var valueFromParent: Int = 1 
    
    // parentVC์— ๊ฐ’ ์ „๋‹ฌ
    @IBAction func sendDataToVc(_ sender: Any) {
        valueFromParent += 1
        let Avc = parent as! AViewController
        let myString = "\(valueFromParent)"
        Avc.dataFromContainer(containerData: myString)
    }
}

[๊ฒฐ๊ณผ ํ™”๋ฉด]

728x90
๋ฐ˜์‘ํ˜•