Skip to content

Latest commit

ย 

History

History
65 lines (45 loc) ยท 4.58 KB

UIViewController.md

File metadata and controls

65 lines (45 loc) ยท 4.58 KB

๋ชจ๋“  View Controller ๊ฐ์ฒด์˜ ์ƒ์œ„ ํด๋ž˜์Šค๋Š” ๋ฌด์—‡์ด๊ณ  ๊ทธ ์—ญํ• ์€ ๋ฌด์—‡์ธ๊ฐ€?

์ฐธ๊ณ ํ•œ ์ข‹์€ ๊ธ€

ํ•จ๊ป˜๋ณด๋ฉด ์ข‹์€ ๋‹ต๋ณ€

๋‹ต๋ณ€

UICollectionViewController, UITableViewController ๋“ฑ ๋ชจ๋“  ViewController์˜ ์ƒ์œ„ํด๋ž˜์Šค๋Š” UIViewController์ž…๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๊ทธ UIViewController์˜ ํ•ต์‹ฌ์ ์ธ ์ƒ์œ„ ํด๋ž˜์Šค๋Š” UIResponder์ž…๋‹ˆ๋‹ค.

์ดํ•˜ ์ง์—ญ

Declaration

@MainActor class UIViewController : UIResponder

Overview

UIViewController ํด๋ž˜์Šค๋Š” ๋ชจ๋“  view controller๋“ค์˜ ๊ณตํ†ต์ ์œผ๋กœ ๋ถ„๋ฐฐ๋˜๋Š” ํ–‰์œ„๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

UIViewController ํด๋ž˜์Šค๋Š” ์•„๋ž˜์˜ ํ–‰์œ„๋“ค์— ๋Œ€ํ•œ ์ฃผ์š” ์ฑ…์ž„์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

  • view ๋‚ด๋ถ€ ๋‚ด์šฉ๋“ค์„ updateํ•œ๋‹ค. ๋ณดํ†ต์€ ๋ฐ์ดํ„ฐ์˜ ๋ณ€๊ฒฝ ๋”ฐ๋ผ์„œ ๋‚ด์šฉ์„ updateํ•œ๋‹ค.
  • view๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉ์ž์™€์˜ ์ƒํ˜ธ์ž‘์šฉ์— ๋Œ€ํ•ด ๋ฐ˜์‘ํ•œ๋‹ค.
  • interface ์ „๋ฐ˜์ ์ธ ๋ ˆ์ด์•„์›ƒ์„ ๊ด€๋ฆฌํ•˜๊ณ  view๋ฅผ ๋ฆฌ์‚ฌ์ด์ง•ํ•œ๋‹ค.
  • ์•ฑ ๋‚ด์˜ ๋‹ค๋ฅธ ๊ฐ์ฒด๋“ค(๋‹ค๋ฅธ view controller ๋“ฑ)๊ณผ ์ƒํ˜ธ์ž‘์šฉ์„ ํ•œ๋‹ค.

view controller๋Š” ๋ทฐ๋“ค๊ณผ ๋‹จ๋‹จํžˆ ๋ฌถ์—ฌ์žˆ์–ด์„œ ๋ทฐ์˜ ๊ณ„์ธต ๊ตฌ์กฐ ์•ˆ์—์„œ event์˜ ์ฒ˜๋ฆฌ๋ฅผ ์ฐธ์—ฌํ•˜๊ณ  ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ํŠนํžˆ, view controller๋Š” UIResponder ๊ฐ์ฒด๋กœ์„œ view controller์˜ root view์™€ ํ•ด๋‹น ๋ทฐ์˜ super view(์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค๋ฅธ view controller์— ์†ํ•˜๋Š”) ์‚ฌ์ด์˜ Responder Chain์— ์‚ฝ์ž…๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ view controller์˜ ๋ทฐ๋“ค ์ค‘ ๊ทธ ๋ˆ„๊ตฌ๋„ event๋ฅผ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, view controller๋Š” ์ด๋ฒคํŠธ๋ฅผ ์ง์ ‘ ์ฒ˜๋ฆฌํ•˜๊ฑฐ๋‚˜ super view์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


๋” ์•Œ์•„๋ณด๊ธฐ UIResponder

@available(iOS 2.0, *)
open class UIResponder : NSObject, UIResponderStandardEditActions {

    open var next: UIResponder? { get }
    
    open var canBecomeFirstResponder: Bool { get } // default is NO
    open func becomeFirstResponder() -> Bool

    open var canResignFirstResponder: Bool { get } // default is YES
    open func resignFirstResponder() -> Bool

    open var isFirstResponder: Bool { get }
    // ...
}

Responder ๊ฐ์ฒด๋Š” ์ด๋ฒคํŠธ๋ฅผ ํ•ธ๋“ค๋งํ•˜๊ณ  ์ด๋ฒคํŠธ์— ๋ฐ˜์‘ํ•  ์ˆ˜ ์žˆ๋Š” UIReponder์˜ ์ธ์Šคํ„ด์Šค์ž…๋‹ˆ๋‹ค. UIApplication, UIViewController ๊ฐ์ฒด๋“ค, ๋ชจ๋“  UIView ๊ฐ์ฒด๋“ค(UIWindow ๊ฐ์ฒด ํฌํ•จ)์„ ํฌํ•จํ•œ ๋งŽ์€ ์ฃผ์š” ๊ฐ์ฒด๋“ค ๋˜ํ•œ reponder์ž…๋‹ˆ๋‹ค. ์ด๋ฒคํŠธ๊ฐ€ ์ผ์–ด๋‚˜๋ฉด, UIKit์€ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋ง์„ ์œ„ํ•ด ํ•ด๋‹น ์ด๋ฒคํŠธ๋ฅผ ์•ฑ์˜ reponder ๊ฐ์ฒด๋“ค์—๊ฒŒ ๋ณด๋‚ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ด๋ฒคํŠธ์˜ ์ข…๋ฅ˜์—” touch events, motion events, remote-control(์›๊ฒฉ์กฐ์ข…) events, ๊ทธ๋ฆฌ๊ณ  press events ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํŠน์ • ์ด๋ฒคํŠธ๋“ค์„ ํ•ธ๋“ค๋งํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” responder๊ฐ€ ํ•ด๋‹น ์ด๋ฒคํŠธ์— ๋Œ€์‘๋˜๋Š” ๋ฉ”์„œ๋“œ๋“ค์„ ์˜ค๋ฒ„๋ผ์ด๋“œํ•˜์—ฌ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํ„ฐ์น˜ ์ด๋ฒคํŠธ๋ฅผ ํ•ธ๋“ค๋งํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” responder๊ฐ€ touchesBegan(_:with:), touchesMoved(_:with:), touchesEnded(_:with:), touchesCancelled(_:with:) ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

ํ„ฐ์น˜์˜ ๊ฒฝ์šฐ์—, responder๋Š” ํ„ฐ์น˜์˜ ๋ณ€ํ™”๋ฅผ ํŠธ๋ž˜ํ‚นํ•˜๊ณ  ์•ฑ์˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ ์ ˆํžˆ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์œ„ํ•ด์„œ UIKit์—์„œ ์ œ๊ณตํ•˜๋Š” ์ด๋ฒคํŠธ ์ •๋ณด๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.

Responder๋“ค์€ UIEvent๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ input view๋ฅผ ํ†ตํ•ด ์ปค์Šคํ…€๋œ input์„ ๋ฐ›์•„๋“ค์ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. system keyboard๊ฐ€ ํ›„์ž์˜ ๊ฒฝ์šฐ๋ฅผ ๊ฐ€์žฅ ์ž˜ ๋‚˜ํƒ€๋‚ด๋Š” ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์œ ์ €๊ฐ€ UITextField๋‚˜ UITextView๋ฅผ ํƒญํ•˜๋ฉด view๋Š” first responder๊ฐ€ ๋˜๊ณ  input view๋ฅผ ๋„์šฐ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ input view๋Š” system keyboard์ด์ฃ .
์ด์™€ ๋น„์Šทํ•˜๊ฒŒ ์šฐ๋ฆฌ๋„ ์ปค์Šคํ…€ input view๋ฅผ ๋งŒ๋“ค๊ณ  ๋‹ค๋ฅธ responder๊ฐ€ ํ™œ์„ฑํ™” ๋  ๋•Œ ์ปค์Šคํ…€๋œ input view๋ฅผ ๋„์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ปค์Šคํ…€ input view๋ฅผ responder์— ์—ฐ๊ฒฐํ•˜๋ ค๋ฉด, ํ•ด๋‹น ๋ทฐ๋ฅผ responder์˜ inputView ํ”„๋กœํผํ‹ฐ์— ํ• ๋‹นํ•˜์„ธ์š”.

+

Responder Chain ๊ตฌ์กฐ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜๊ณ , First Responder ์—ญํ• ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜์‹œ์˜ค.