iOS

[ iOS ] ๋‚ด๋น„๊ฒŒ์ด์…˜ ์ปจํŠธ๋กค๋Ÿฌ (2) - ๊ธฐ๋Šฅ

Forest Yun 2021. 8. 30. 22:21
728x90

๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

 

๋ฌธ์ œ

๋™์ž‘ ํ™”๋ฉด์€ '๋ฉ”์ธํ™”๋ฉด'๊ณผ '์ˆ˜์ •ํ™”๋ฉด' ๋‘ ๊ฐœ๋กœ ๋งŒ๋“ ๋‹ค.  ๋˜ํ•œ '๋ฉ”์ธํ™”๋ฉด'์—์„œ '์ˆ˜์ •ํ™”๋ฉด'์œผ๋กœ, '์ˆ˜์ •ํ™”๋ฉด'์—์„œ '๋ฉ”์ธํ™”๋ฉด'์œผ๋กœ ๋ฉ”์‹œ์ง€(๋ฐ์ดํ„ฐ)๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๊ณ  ์ด๋ฏธ์ง€์˜ ํ™•๋Œ€ ์—ฌ๋ถ€๋ฅผ '์ˆ˜์ •ํ™”๋ฉด'์—์„œ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. 

 

 

๊ตฌํ˜„ ์ˆœ์„œ

  1. ๋‚ด๋น„๊ฒŒ์ด์…˜ ์ปจํŠธ๋กค๋Ÿฌ ์ถ”๊ฐ€
  2. '๋ฉ”์ธํ™”๋ฉด' ๋ ˆ์ด์•„์›ƒ ๊ตฌ์„ฑ
  3. '์ˆ˜์ •ํ™”๋ฉด' ์ถ”๊ฐ€ ๋ฐ ๋ ˆ์ด์•„์›ƒ ๊ตฌ์„ฑ
  4. ๋ทฐ ์ „ํ™˜๊ณผ ํ•จ๊ป˜ ๋ฉ”์‹œ์ง€ ์ „๋‹ฌํ•˜๊ธฐ
  5. '์ˆ˜์ •ํ™”๋ฉด'์—์„œ '๋ฉ”์ธํ™”๋ฉด'์˜ ์ด๋ฏธ์ง€ ์ œ์–ดํ•˜๊ธฐ

 

 

์ตœ์ข… ๊ฒฐ๊ณผ

 

 

 

 

 

 

 

 

 

 

 

 


4. ๋ทฐ ์ „ํ™˜๊ณผ ํ•จ๊ป˜ ๋ฉ”์‹œ์ง€ ์ „๋‹ฌํ•˜๊ธฐ

 

4.1. ์•„์šธ๋ › ๋ณ€์ˆ˜์™€ ์•ก์…˜ ํ•จ์ˆ˜ ์ถ”๊ฐ€ํ•˜๊ธฐ

 

'๋ฉ”์ธํ™”๋ฉด'

    @IBOutlet var txMessage: UITextField!
    @IBOutlet var imgView: UIImageView!
  • Text Field์— ์ ํžŒ ๋ฉ”์‹œ์ง€๋ฅผ ํ™•์ธ ๋ฐ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•œ text ์†์„ฑ ์‚ฌ์šฉ ์˜ˆ์ •
  • imageView์˜ ํ™•๋Œ€ ๋ฐ ์ถ•์†Œ ์†์„ฑ ์ œ์–ด ๋ชฉ์ 

 

 

 

'์ˆ˜์ •ํ™”๋ฉด'

    @IBOutlet var txMessage: UITextField!
    @IBOutlet var swIsOn: UISwitch!
    
    ...

    @IBAction func btnDone(_ sender: UIButton) {
    }
  • Text Field์— ์ ํžŒ ๋ฉ”์‹œ์ง€๋ฅผ ํ™•์ธ ๋ฐ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•œ text ์†์„ฑ ์‚ฌ์šฉ ์˜ˆ์ •
  • switch ์ƒํƒœ ํ™•์ธ ๋ฐ ์„ค์ • ๋ชฉ์ 
  • '์™„๋ฃŒ' ๋ฒ„ํŠผ ํด๋ฆญ์‹œ ํ™”๋ฉด ์ „ํ™˜ ๋ฐ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ ๋ชฉ์ 

 

 

 

 


 

 

 

 

4.2 '๋ฉ”์ธํ™”๋ฉด' -> '์ˆ˜์ •ํ™”๋ฉด' ๋ฐ์ดํ„ฐ ์ „๋‹ฌ

'๋ฉ”์ธํ™”๋ฉด'์—์„œ prepareํ•จ์ˆ˜๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•˜์—ฌ EditMissionViewController, ์ฆ‰ '์ˆ˜์ •ํ™”๋ฉด'์„ ๋„์ฐฉ ์„ธ๊ทธ์›จ์ด๋กœ ์„ค์ •ํ•œ๋‹ค.

๊ทธ ํ›„ '๋ฉ”์ธํ™”๋ฉด'์˜ Text Field์˜ ๊ฐ’์„ '์ˆ˜์ •ํ™”๋ฉด'์˜ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ๋‹ค. '์ˆ˜์ •ํ™”๋ฉด'์—์„œ๋Š” ๋ณ€์ˆ˜์˜ ๊ฐ’์„ Text Field์— ํ• ๋‹นํ•œ๋‹ค.

 

 

EditMissionViewController

'๋ฉ”์ธํ™”๋ฉด'์—์„œ Text Field์˜ text๋ฅผ ์ง์ ‘ ์ œ์–ดํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•ด '๋ฉ”์ธํ™”๋ฉด'์—์„œ ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.

class EditMissionViewController: UIViewController {
    var textMessage: String = "" 
    //'๋ฉ”์ธํ™”๋ฉด'์—์„œ Text Field์˜ text๋ฅผ ์ง์ ‘ ์ œ์–ดํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•ด 
    //'๋ฉ”์ธํ™”๋ฉด'์—์„œ ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.

...

}

 

 

'๋ฉ”์ธํ™”๋ฉด'์—์„œ '์ˆ˜์ •ํ™”๋ฉด'์œผ๋กœ์˜ ํ™”๋ฉด ์ „ํ™˜๊ณผ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ์„ ์œ„ํ•ด prepare ํ•จ์ˆ˜๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•œ๋‹ค.

 

prepare ํ•จ์ˆ˜ : ํ•ด๋‹น ์„ธ๊ทธ์›จ์ด๊ฐ€ ํ•ด๋‹น ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์ „ํ™˜๋˜๊ธฐ ์ง์ „์— ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜์ด๋ฉฐ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
์Šคํ† ๋ฆฌ๋ณด๋“œ์—์„œ ํ™”๋ฉด ์ „ํ™˜ ์ „, ์ž‘์€ ์ค€๋น„๋ฅผ ์›ํ•  ๊ฒƒ์ด๋‹ค.

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
segue.destination๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค. ๋ทฐ ์ „ํ™˜
        // Pass the selected object to the new view controller.
์ƒˆ ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์„ ํƒ๋œ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•œ๋‹ค. ๋ฐ์ดํ„ฐ ์ „๋‹ฌ
    }
    //ํ•ด๋‹น ์„ธ๊ทธ์›จ์ด๊ฐ€ ํ•ด๋‹น ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์ „ํ™˜๋˜๊ธฐ ์ง์ „์— ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜์ด๋ฉฐ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.
    //'๋ฉ”์ธํ™”๋ฉด'-> '์ˆ˜์ •ํ™”๋ฉด' ํ™”๋ฉด ์ „ํ™˜ ๋ฐ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let editMissionViewController = segue.destination as! EditMissionViewController
        //์„ธ๊ทธ์›จ์ด์˜ ๋„์ฐฉ ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ EditMissionViewController๋กœ ํ•œ๋‹ค.
        editMissionViewController.textMessage = txMessage.text!
    }

 

 

 


 

 

 

 

4.3 '์ˆ˜์ •ํ™”๋ฉด' -> '๋ฉ”์ธํ™”๋ฉด' ํ™”๋ฉด ์ „ํ™˜ ๋ฐ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ

ํ”„๋กœํ† ์ฝœ ํ˜•ํƒœ์˜ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ์„ ํ•ด๋ณด์ž

didMessageEditDone ํ•จ์ˆ˜๋Š” '์ˆ˜์ •ํ™”๋ฉด'์—์„œ ๋ฉ”์‹œ์ง€๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด '๋ฉ”์ธํ™”๋ฉด'์— ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š” didMessageEditDone ํ•จ์ˆ˜๊ฐ€ ์‘๋‹ตํ•˜์—ฌ ์ด ๋ฉ”์‹œ์ง€์˜ ๋ฌธ์ž์—ด ๊ฐ’์„ '๋ฉ”์ธํ™”๋ฉด'์˜ ํ…์ŠคํŠธ ํ•„๋“œ์— ํ…์ŠคํŠธ๋กœ ๋ณด์—ฌ์ค€๋‹ค. 

์ฆ‰, '์ˆ˜์ •ํ™”๋ฉด'์—์„œ '๋ฉ”์ธํ™”๋ฉด'์œผ๋กœ ๋ฐ์ดํ„ฐ ์ „๋‹ฌํ•œ๋‹ค.

import UIKit

protocol EditMissionDelegate {
    //EditViewController์—์„œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉฐ ๋ฉ”์‹œ์ง€๋ฅผ ์ „๋‹ฌํ•ด ์ฃผ๋Š”๋ฐ, ์ด ๋ฉ”์‹œ์ง€์˜ ๋ฌธ์ž์—ด ๊ฐ’์„ '๋ฉ”์ธํ™”๋ฉด'์˜ ํ…์ŠคํŠธ ํ•„๋“œ์— ํ…์ŠคํŠธ๋กœ ๋ณด์—ฌ์ค€๋‹ค.
    //์ฆ‰, '์ˆ˜์ •ํ™”๋ฉด'์—์„œ '๋ฉ”์ธํ™”๋ฉด'์œผ๋กœ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ
    func didMessageEditDone(_ controller: EditMissionViewController, message: String)
}

class EditMissionViewController: UIViewController {

	...

}

 

'๋ฉ”์ธํ™”๋ฉด'์— didMessageEditDone ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์ „, EditMissionDelegate ํ”„๋กœํ† ์ฝœ์„ ๋จผ์ € ์ฑ„ํƒํ•œ๋‹ค. ๊ทธ ํ›„ didMessageEditDone ํ•จ์ˆ˜์— Text Field์˜ text์— ์ „๋‹ฌ ์ธ์ž๋ฅผ ํ• ๋‹นํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

 

class NavigationMissionViewController: UIViewController, EditMissionDelegate {
 
 ...
 
  func didMessageEditDone(_ controller: EditMissionViewController, message: String) {
        txMessage.text = message
    }
    
    ...
 
}

 

+ ํ”„๋กœํ† ์ฝœ์— ๋Œ€ํ•œ ๊ฐ„๋‹จํ•œ ์„ค๋ช…

https://88yhtserof.tistory.com/23?category=954886 

 

[ IOS ] ํ”ผ์ปค ๋ทฐ ๋งŒ๋“ค๊ธฐ ( ํ”„๋กœํ† ์ฝœ, ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๋ฉ”์„œ๋“œ )

๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ๋ฌธ์ œ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋‘ ๊ฐœ์ธ ํ”ผ์ปค ๋ทฐ๋ฅผ ๊ตฌํ˜„ํ•˜๋ผ. ์™ผ์ชฝ ํ”ผ์ปค ๋ทฐ๋ฅผ ์„ ํƒํ–ˆ์„ ๊ฒฝ์šฐ ํŒŒ์ผ ๋ช…์ด ๋‚˜ํƒ€๋‚˜๋„๋ก, ์˜ค๋ฅธ์ชฝ ํ”ผ์ปค ๋ทฐ๋ฅผ ์„ ํƒํ–ˆ์„ ๊ฒฝ์šฐ ์ด๋ฏธ์ง€ ๋ทฐ์— ์ด๋ฏธ์ง€๋ฅผ ์ถœ

88yhtserof.tistory.com

 

 

 

 

 

'๋ฉ”์ธํ™”๋ฉด'์— ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š” didMessageEditDoneํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด prepare ํ•จ์ˆ˜์—์„œ ๋„์ฐฉ ์„ธ๊ทธ์›จ์ด, ์ฆ‰ editMissionVieController('์ˆ˜์ •ํ™”๋ฉด')์— ์„ ์–ธ๋œ ๋ณ€์ˆ˜ delegate์— self๋ฅผ ํ• ๋‹นํ•œ๋‹ค. ๊ทธ ๋ง์€ NavigationMissionViewController('๋ฉ”์ธํ™”๋ฉด')๋ฅผ ํ• ๋‹นํ•œ๋‹ค๋Š” ๋ง์ด๋‹ค.

class NavigationMissionViewController: UIViewController, EditMissionDelegate {
    
    ...
    
    //ํ•ด๋‹น ์„ธ๊ทธ์›จ์ด๊ฐ€ ํ•ด๋‹น ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์ „ํ™˜๋˜๊ธฐ ์ง์ „์— ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜์ด๋ฉฐ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.
    //'๋ฉ”์ธํ™”๋ฉด'-> '์ˆ˜์ •ํ™”๋ฉด' ํ™”๋ฉด ์ „ํ™˜ ๋ฐ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let editMissionViewController = segue.destination as! EditMissionViewController //์„ธ๊ทธ์›จ์ด์˜ ๋„์ฐฉ ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ EditMissionViewController๋กœ ํ•œ๋‹ค.
        editMissionViewController.textMessage = txMessage.text!
        
        editMissionViewController.delegate = self
    }
    
    ...

}

 

'๋ฉ”์ธํ™”๋ฉด'์—์„œ '์ˆ˜์ •ํ™”๋ฉด'์œผ๋กœ ํ™”๋ฉด ์ „ํ™˜์„ ํ•  ๋•Œ, delegate๋ฅผ NavigationMissionViewController('๋ฉ”์ธํ™”๋ฉด')๋ฅผ ํ• ๋‹นํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— '์ˆ˜์ •ํ™”๋ฉด'์—์„œ '๋ฉ”์ธํ™”๋ฉด'์— ๊ตฌํ˜„๋œ didMessageEditDoneํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

'์ˆ˜์ •ํ™”๋ฉด'์—์„œ '์™„๋ฃŒ' ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ '๋ฉ”์ธํ™”๋ฉด'์œผ๋กœ ์ „ํ™˜ํ•˜๋‹ˆ๊น, '์™„๋ฃŒ' ๋ฒ„ํŠผ์˜ ์•ก์…˜ ํ•จ์ˆ˜์ธ btnDone์— didMessageEditDoneํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

class EditMissionViewController: UIViewController {
    
    var textMessage: String = ""  //'๋ฉ”์ธํ™”๋ฉด'์—์„œ Text Fielf์˜ text๋ฅผ ์ง์ ‘ ์ œ์–ดํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•ด '๋ฉ”์ธํ™”๋ฉด'์—์„œ ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.
    var delegate:EditMissionDelegate?
    
    
    @IBOutlet var txMessage: UITextField!
    @IBOutlet var swIsOn: UISwitch!
    
   ...

    @IBAction func btnDone(_ sender: UIButton) {
        if delegate != nil {
            delegate!.didMessageEditDone(self, message: txMessage.text!)
        }
        
        _ = navigationController?.popViewController(animated: true)
        //๋‚ด๋น„๊ฒŒ์ด์…˜ ์Šคํƒ์—์„œ top ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์‚ญ์ œ ํ›„ ๋ฐ˜ํ™˜ํ•œ๋‹ค(pop). ๊ทธ๋ฆฌ๊ณ  ํ™”๋ฉด์„ ๊ฐฑ์‹ ํ•œ๋‹ค.
        //์™€์ผ๋“œ ์นด๋“œ ์‹๋ณ„์ž๋Š” ๋ฐ˜ํ™˜๊ฐ’์€ ์žˆ์ง€๋งŒ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค
    }
   ...

}

 

 

'์ˆ˜์ •ํ™”๋ฉด'์—์„œ '๋ฉ”์ธํ™”๋ฉด'์œผ๋กœ์˜ ์ „ํ™˜์„ ์œ„ํ•ด navigationController?.popViewController()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

func popViewController(animated: Bool) -> UIViewController?
->> Pops the top view controller from the navigation stack and updates the display.

๋‚ด๋น„๊ฒŒ์ด์…˜ ์Šคํƒ์—์„œ top ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์‚ญ์ œ ํ›„ ๋ฐ˜ํ™˜ํ•œ๋‹ค(pop). ๊ทธ๋ฆฌ๊ณ  ํ™”๋ฉด์„ ๊ฐฑ์‹ ํ•œ๋‹ค.

์—ฌ๊ธฐ์„œ ์™€์ผ๋“œ ์นด๋“œ ์‹๋ณ„์ž๋Š” ๋ฐ˜ํ™˜๊ฐ’์€ ์žˆ์ง€๋งŒ ๊ทธ ๋ฐ˜ํ™˜๊ฐ’์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

 

 

 

 

 

 

 

 

 

 

 

 

 


5. '์ˆ˜์ •ํ™”๋ฉด'์—์„œ '๋ฉ”์ธํ™”๋ฉด'์˜ ์ด๋ฏธ์ง€ ์ œ์–ดํ•˜๊ธฐ

 

5.1 '๋ฉ”์ธํ™”๋ฉด'์—์„œ์˜ ์ด๋ฏธ์ง€ ์ƒํƒœ๋ฅผ '์ˆ˜์ •ํ™”๋ฉด'์— ์ „๋‹ฌํ•˜๊ธฐ

'๋ฉ”์ธํ™”๋ฉด'์—์„œ '์ˆ˜์ •ํ™”๋ฉด'์˜ switch๋ฅผ ์ง์ ‘์ ์œผ๋กœ ์ œ์–ดํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ, '์ˆ˜์ •ํ™”๋ฉด'์— switch ์ƒํƒœ ์—ฌ๋ถ€๋ฅผ ์ €์žฅํ•  'isZoom' ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  viewDidLoad()ํ•จ์ˆ˜์—์„œ switch.isOn์— 'isZoom' ๋ณ€์ˆ˜๋ฅผ ํ• ๋‹นํ•œ๋‹ค.

class EditMissionViewController: UIViewController {
    
    ...
    
    var isZoom: Bool = false //'๋ฉ”์ธํ™”๋ฉด'์—์„œ '์ˆ˜์ •ํ™”๋ฉด'์˜ switch๋ฅผ ์ง์ ‘ ์ œ์–ดํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•ด '๋ฉ”์ธํ™”๋ฉด'์—์„œ ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.
    
    
    @IBOutlet var txMessage: UITextField!
    @IBOutlet var swIsOn: UISwitch!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        txMessage.text = textMessage//๋ณ€์ˆ˜ message๊ฐ’์€ txMessage์˜ text์— ํ• ๋‹นํ•ด ์ถœ๋ ฅ๋˜๋„๋ก ํ•œ๋‹ค.
        swIsOn.isOn = isZoom
    }

...

}

 

๊ทธ ํ›„ '๋ฉ”์ธํ™”๋ฉด'์—์„œ๋„ 'isZoom' ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  prepare ํ•จ์ˆ˜์—์„œ, '๋ฉ”์ธํ™”๋ฉด'์˜ 'isZoom' ๋ณ€์ˆ˜๊ฐ’์„ '์ˆ˜์ •ํ™”๋ฉด'์˜ 'isZoom'์— ํ• ๋‹นํ•œ๋‹ค.

class NavigationMissionViewController: UIViewController, EditMissionDelegate {
    
    let scale: CGFloat = 2.0
    var newWidth: CGFloat = 0.0
    var newHeight: CGFloat = 0.0
    var isZoom:Bool = false
    
    
    
    @IBOutlet var txMessage: UITextField!
    @IBOutlet var imgView: UIImageView!
    
    ...
    
    //ํ•ด๋‹น ์„ธ๊ทธ์›จ์ด๊ฐ€ ํ•ด๋‹น ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์ „ํ™˜๋˜๊ธฐ ์ง์ „์— ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜์ด๋ฉฐ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.
    //'๋ฉ”์ธํ™”๋ฉด'-> '์ˆ˜์ •ํ™”๋ฉด' ํ™”๋ฉด ์ „ํ™˜ ๋ฐ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let editMissionViewController = segue.destination as! EditMissionViewController //์„ธ๊ทธ์›จ์ด์˜ ๋„์ฐฉ ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ EditMissionViewController๋กœ ํ•œ๋‹ค.
        editMissionViewController.textMessage = txMessage.text!
        editMissionViewController.isZoom = isZoom
        editMissionViewController.delegate = self
    }
    
    ...
}

 

 

 

 


 

 

 

 

 

 

5.2 '์ˆ˜์ •ํ™”๋ฉด'์—์„œ์˜ ์ด๋ฏธ์ง€ ์ƒํƒœ๋ฅผ '๋ฉ”์ธํ™”๋ฉด'์— ์ „๋‹ฌํ•˜๊ธฐ

'์ˆ˜์ •ํ™”๋ฉด'์—์„œ '๋ฉ”์ธํ™”๋ฉด'๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด๊ธฐ ์œ„ํ•ด ํ”„๋กœํ† ์ฝœ ํ˜•ํƒœ์˜ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. (didImageZoomDone)

didImageZoomDone ํ•จ์ˆ˜๋Š” ์ด๋ฏธ์ง€์˜ ํ™•๋Œ€ ์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋‚ผ isZoom์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ฐ–๋Š”๋‹ค.

import UIKit

protocol EditMissionDelegate {
    //์ˆ˜์ •ํ™”๋ฉด'์—์„œ '๋ฉ”์ธํ™”๋ฉด'์œผ๋กœ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ
    func didMessageEditDone(_ controller: EditMissionViewController, message: String)
    func didImageZoomDone(_ controller: EditMissionViewController, isZoom: Bool)
}

class EditMissionViewController: UIViewController {
    
    ...
    
}

 

 

'๋ฉ”์ธํ™”๋ฉด'์— didImageZoomDone ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. isZoom ์ „๋‹ฌ ์ธ์ž๋ฅผ '๋ฉ”์ธํ™”๋ฉด'์˜ ๋ณ€์ˆ˜์ธ isZoom์— ํ• ๋‹นํ•œ๋‹ค. ์ฆ‰, '์ˆ˜์ •ํ™”๋ฉด'์—์„œ '๋ฉ”์ธํ™”๋ฉด'์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•œ ๊ฒƒ์ด๋‹ค.

์ „๋‹ฌ์ธ์ž isZoom์ด true์ธ ๊ฒฝ์šฐ ํ™•๋Œ€๋ฅผ, false์ธ ๊ฒฝ์šฐ ์ถ•์†Œ๋ฅผ ํ•œ๋‹ค.

class NavigationMissionViewController: UIViewController, EditMissionDelegate {
    
    let scale: CGFloat = 2.0
    var newWidth: CGFloat = 0.0
    var newHeight: CGFloat = 0.0
    var isZoom:Bool = true
    
    ...
    
    func didImageZoomDone(_ controller: EditMissionViewController, isZoom: Bool) {
        //'๋ฉ”์ธํ™”๋ฉด'์˜ ๋ณ€์ˆ˜์ธ isZoom์— ์ ‘๋‹ฌ์ธ์ž isZoom์„ ํ• ๋‹น
        if isZoom {
            newWidth = imgView.frame.width*scale
            newHeight = imgView.frame.height*scale
            self.isZoom = true
        }else {
            newWidth = imgView.frame.width/scale
            newHeight = imgView.frame.height/scale
            self.isZoom = false
        }
        imgView.frame.size = CGSize(width: newWidth, height: newHeight)
    }
    
    ...

}

 

 

'์ˆ˜์ •ํ™”๋ฉด'์—์„œ, switch ์•ก์…˜ํ•จ์ˆ˜์— OnOff์— ๋”ฐ๋ผ isZoom ๋ณ€์ˆ˜์˜ ๊ฐ’์ด ๊ฒฐ์ •๋˜๊ฒŒ ํ•œ๋‹ค.

class EditMissionViewController: UIViewController {
    
    ...
    
    @IBAction func swImageZoom(_ sender: UISwitch) {
        if sender.isOn {
            isZoom = true
        }else {
            isZoom = false
        }
        
    }
    
   ...

}

 

 

 

'์ˆ˜์ •ํ™”๋ฉด'์—์„œ '๋ฉ”์ธํ™”๋ฉด'์œผ๋กœ ๋„˜์–ด๊ฐ€๋Š” ๋ฒ„ํŠผ์ธ '์™„๋ฃŒ' ๋ฒ„ํŠผ์˜ ์•ก์…˜ ํ•จ์ˆ˜์— didImageZoomDone ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

class EditMissionViewController: UIViewController {
   ...
   
   
    @IBAction func btnDone(_ sender: UIButton) {
        //์—ฌ๊ธฐ์„œ delegate๋Š” '๋ฉ”์ธํ™”๋ฉด'์„ ์˜๋ฏธ
        if delegate != nil {
            delegate?.didMessageEditDone(self, message: txMessage.text!)
            delegate?.didImageZoomDone(self, isZoom: isZoom)
        }
        
        _ = navigationController?.popViewController(animated: true)
        //๋‚ด๋น„๊ฒŒ์ด์…˜ ์Šคํƒ์—์„œ top ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์‚ญ์ œ ํ›„ ๋ฐ˜ํ™˜ํ•œ๋‹ค(pop). ๊ทธ๋ฆฌ๊ณ  ํ™”๋ฉด์„ ๊ฐฑ์‹ ํ•œ๋‹ค.
        //์™€์ผ๋“œ ์นด๋“œ ์‹๋ณ„์ž๋Š” ๋ฐ˜ํ™˜๊ฐ’์€ ์žˆ์ง€๋งŒ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค
    }
   ...

}

 

 

 

 

 

 

 

 

 

 

 

๊ณต๋ถ€ ์ž๋ฃŒ

Do It! ์Šค์œ„ํ”„ํŠธ๋กœ ์•„์ดํฐ ์•ฑ ๋งŒ๋“ค๊ธฐ ๊ฐœ์ • 5ํŒ
SWIFT ๋ฌธ์„œ
728x90