Skip to content

Instantly share code, notes, and snippets.

@cloudiosx
Created March 22, 2022 04:56
Show Gist options
  • Save cloudiosx/7c18f62017be75eb5d5fd1156d5c2d18 to your computer and use it in GitHub Desktop.
Save cloudiosx/7c18f62017be75eb5d5fd1156d5c2d18 to your computer and use it in GitHub Desktop.

Introduction

Design Patterns that will be covered in this article:

  • Model-View-Controller

Model-View-Controller

The MVC pattern simply is broken down into 3 main parts:

  1. Model
    • Models hold onto application data (typically in the form of structs or simple classes)
  2. View
    • Views communicate to their controller via IBActions
  3. View Controller
    • Controllers coordinate between models and views

Important to Note

  • Models and views should not hold strong references to their owning controller because this would cause a retain cycle
  • Use the MVC design pattern as a starting point and be open to implementing other patterns as you build your app

Code

Screen Shot 2022-03-21 at 11.31.37 PM.png

  • When implementing a MVC design pattern, it's convenient to organize your app bundle's files with a Model, View, and Controller directory.

Model

Screen Shot 2022-03-21 at 11.38.30 PM.png

  • I created a simple struct that represents a single programmer

View

Screen Shot 2022-03-21 at 11.42.00 PM.png

  • I created an IBOutlet for each of the properties I have in the Programmer model

Screen Shot 2022-03-22 at 12.47.37 AM.png

  • I set the class of the default UIView element to the view I set-up programmatically (ProgrammerView).
  • I dragged 3 UITextFields from our Object Library and placed it within the UIView.
  • Go ahead and connect the 3 UITextFields with the IBOutlets using the Connection Inspector of the UIView.
  • Make sure to set Auto Layout constraints for your UI elements.

Controller

Screen Shot 2022-03-22 at 12.02.53 AM.png

  • In your main view controller, you can hold a strong reference to the view and model that it owns.

Screen Shot 2022-03-22 at 12.14.18 AM.png

  • It's the controller's job to coordinate between the model and view.
    • In this case, the controller should update its view using the values from the model.
public var programmer: Programmer? {
  didSet {
    updateViewFromProgrammer()
  }
}
  • If a programmer is set after viewDidLoad() is called, the controller should update ProgrammerView.
    • You can use a didSet to accomplish this.
  • This is an example of how the model can tell the controller that something has changed and that the view needs updating.

Screen Shot 2022-03-22 at 12.29.21 AM.png

  • Conversely, this is an example of how the view can tell the controller that something has changed and the model needs updating.
  • I connected the IBAction to all three of the textfields in Main.storyboard.

Screen Shot 2022-03-22 at 12.49.37 AM.png

  • To see the IBAction work with a UITextField, I connected each of the textfields to the same IBAction with an Editing Changed event.

Cautions

Not every object fits into a category of a model, view, or controller

Be-ware of the "Massive-View-Controller" or view controllers growing too big

Feel free to use other design patterns to build on top of MVC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment