스탠포드 iOS 강의노트 L6,7

Lecture 6,7

Multiple MVCs, LifeCycle, ARC, Protocol, Delegation

Course Description Updated for iOS 10 and Swift. Tools and APIs required to build applications for the iPhone and iPad platforms using the iOS SDK. User interface design for mobile devices and unique user interactions using multi-touch technologies. Object-oriented design using model-view-controller paradigm, memory management, Swift programming language. Other topics include: object-oriented database API, animation, mobile device power management, multi-threading, networking and performance considerations.

Multiple MVCs

import UIKit
class EmotionsViewController: VCLLoggingViewController
{
	// override func prepare(for segue: )
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        var destinationViewController = segue.destination
        
        if let navigationController = destinationViewController as? UINavigationController {
            destinationViewController = navigationController.visibleViewController ?? destinationViewController
        }
        
        // 화면 전환에 따른 expression 변화
        if let faceViewController = destinationViewController as? FaceViewController,
            let identifier = segue.identifier,
            let expression = emotionalFaces[identifier] {
                 faceViewController.expression = expression
            faceViewController.navigationItem.title = (sender as? UIButton)?.currentTitle
                }
            }
 	
 	// segue identifier와 FacialExpression(Model)의 case 매칭하여 Dic 형태로 저장
   
    private let emotionalFaces: Dictionary<String,FacialExpression> = [
        "happy": FacialExpression(eyes: .open, mouth: .smile),
        "sad": FacialExpression(eyes: .open, mouth: .frown),
        "worried": FacialExpression(eyes: .closed, mouth: .smirk)
    ]
}

View Controller LifeCycle (요약)

Automatic Reference Counting

Influencing ARC

Protocols

Delegation

How it plays out

  1. A View declares a delegation protocol (what the View wants the controller to do for it)
  2. The View’s API has a weak delegate property whose type is that delegation protocol
  3. The View uses the delegate property to get/do things it can’t own or control on its own
  4. The Controller declares that it implements the protocol
  5. The Controller sets self as the delegate of the View by setting the property in @2
  6. The Controller implements the protocol (probably it has lots of optional methods in it)

Scroll View