ViewController๋Š” iOS ์•ฑ ๊ฐœ๋ฐœ์—์„œ ๋นผ๋†“์„ ์ˆ˜ ์—†๋Š” ์š”์†Œ์ด๋‹ค. ๊ตฌ์กฐ์ ์œผ๋กœ ์–ด๋–ป๊ฒŒ ๊ตฌ์„ฑ๋˜์—ˆ๋Š”์ง€ ์•Œ์•„๋ณด๊ณ , ์–ด๋–ค life cycle์„ ๊ฐ€์ง€๋Š”์ง€ ์ดํ•ดํ•ด๋ณด์ž. ๊ทธ๋Ÿผ ์‹œ์ž‘ํ•˜์ž.

ViewController

  • View management model

  • ์—ญํ• 

    • view ๊ตฌ์„ฑ
    • view event ์ฒ˜๋ฆฌ
    • data๋ฅผ view์™€ ์—ฐ๊ฒฐ
  • templates

    class ViewController: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
            // VC๋‚ด๋ถ€์— property๋กœ ์žˆ๋Š” view๊ฐ€ ํ™”๋ฉด์— ๋ณด์ด๊ธฐ ์œ„ํ•ด load ๋˜์—ˆ์„ ๋•Œ ํ˜ธ์ถœ๋จ
            // VC ์ƒ์„ฑ ์‹œ์ ๊ณผ view๊ฐ€ ๋ณด์ด๋Š” ์‹œ์ ์€ ์ฐจ์ด๊ฐ€ ์žˆ์Œ
        }
     
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // App์ด ๋ฉ”๋ชจ๋ฆฌ ๋ถ€์กฑ ๊ฒฝ๊ณ ๋ฅผ ๋ฐ›์„ ๊ฒฝ์šฐ ํ˜ธ์ถœ๋จ
        }
    }
    • ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ๋Š” AppDelegate์—์„œ๋„ ๊ฐ€๋Šฅ

์ดˆ๊ธฐํ™”

// code
public init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?)
 
// storyboard
public init?(coder: NSCoder)
  • parameter
    • nibName: nib ํŒŒ์ผ๋ช…
    • bundle: nibํŒŒ์ผ์ด ์žˆ๋Š” Bundle(NSBundle) ๊ฐ์ฒด
    • UIViewController.init()์ธ ๊ฒฝ์šฐ nil๋กœ ๋„˜์–ด์˜ด
  • ํ™œ์šฉ
    class ViewController: UIViewController {
        override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
            super.init(nibName: nibNameOrnil, bundle: nibBundleOrNil)
     
            // ์ดˆ๊ธฐํ™” ์ฝ”๋“œ ์ž‘์„ฑ
            // ํ•ด๋‹น ์‹œ์ ์—๋Š” view๊ฐ€ ์—†์Œ
            // view์™€ ๊ด€๊ณ„์—†๋Š” object ์ดˆ๊ธฐํ™” ์šฉ๋„๋กœ ์ข‹์Œ
        }
    }
     
    class ViewController: UIViewController {
        required init?(coder: NSCoder) {
            super.init(coder: coder)
     
            // ์ƒ๋“ฑ
        }
    }

Life Cycle

  1. init
  2. loadView
  3. viewDidLoad
  4. viewWillAppear
  5. viewDidAppear
  6. viewWillDisappear
  7. viewDidDisappear
  8. deinit

LoadView

  • view์— ๋Œ€ํ•œ customizing ์ž‘์—…
  • ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•„๋„ ๊ธฐ๋ณธ์ ์œผ๋กœ view๊ฐ€ ์ƒ์„ฑ๋˜์–ด load๋จ
override func loadView() {
    let view = UIView(frame: UIScreen.main.bounds) // ์Šคํฌ๋ฆฐ ํฌ๊ธฐ์— ๋งž์ถ˜ view ์ƒ์„ฑ
    view.backgroundColor = UIColor.green
    view.autoresizingMask = [.flexibleWidth, .flexibleHeight] // window์˜ ํฌ๊ธฐ ๋ณ€๊ฒฝ์— ๋งž์ถฐ ๋ณ€ํ•˜๋„๋ก ์„ค์ •
    self.view = view
}

์ฃผ์˜์‚ฌํ•ญ

  • init method์—์„œ view์— ์ ‘๊ทผํ•˜๋ฉด ์œ„ํ—˜ํ•จ
    • ์ดˆ๊ธฐํ™” ์ž‘์—… ์ค‘์— viewDidLoad๊ฐ€ ํ˜ธ์ถœ๋  ์ˆ˜ ์žˆ์Œ
    • method ํ˜ธ์ถœ ์ˆœ์„œ๊ฐ€ ๋‹ฌ๋ผ์ ธ ๋ฒ„๊ทธ ๋ฐœ์ƒ์œ„ํ—˜ ์žˆ์Œ
    • ์ผ๋ฐ˜์ ์ธ ์ƒํ™ฉ
      • viewController๊ฐ€ ์‚ฌ์šฉ๋˜๊ธฐ ์‹œ์ž‘ํ•  ๋•Œ ํ˜ธ์ถœ๋จ
    • init method์—์„œ ์ ‘๊ทผ
      • view ์ƒ์„ฑ, ์ดˆ๊ธฐํ™” ๊ณผ์ •์—์„œ ํ˜ธ์ถœ๋จ

Container View Controller

  • UIViewController ํ•˜์œ„์— ChildViewController๋ฅผ ๋‘˜ ์ˆ˜ ์žˆ์Œ
  • ๊ธฐ๋Šฅ์— ๋”ฐ๋ผ ViewController๋ฅผ ๋ถ„๋ฆฌ ํ›„, Container ViewController์— ๋ชจ์•„์„œ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ์Œ
  • ๋™์ผํ•œ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๊ฐ€์ง

์ •๋ฆฌ

  • UIWindow์œ„์— rootViewController๊ฐ€ ์˜ฌ๋ผ๊ฐ„๋‹ค.
  • ViewController์—์„œ๋Š” ChildViewController๋ฅผ ๊ฐ€์ง„๋‹ค.
  • ์ถ”๊ฐ€์ ์œผ๋กœ ๋‹ค๋ฅธ ์ฐฝ์„ ๋„์šธ์ˆ˜๋„ ์žˆ๊ณ (modal) ์ด๋Š” presentedViewController๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค.

Reference