• 10 hours
  • Medium

Free online content available in this course.

Videos available in this course

Certificate of achievement available at the end this course

Got it!

Connect the dots in M-V-C

Log in or subscribe for free to enjoy all this course has to offer!

Hey-ya! Moving on! We’ve got our view and model implementation, only the linking element is missing! This is our controller:

MVC - Controller
MVC - Controller

Our controller is not aware of the view and not doing anything useful with the model for the moment.

Connecting View with the code

As we established earlier in this course, the primary responsibility of the ViewController (ViewController.swift file) is to control the view. So let's make it be aware of all the elements we have composed within our interface.

We are going to create 2 types of connections:

  • elements to variables

  • actions to functions

Creating IBOutlets

Let's switch to assistant view with the Main Storyboard on the left and the ViewController on the right (you can hide the Navigator and Utilities panel to make more space):

Assistant view - Main screen & ViewController
Assistant view - Main storyboard & ViewController

Select the frame view and right click on it to display the context menu:

UI element context menu
UI element context menu

Select the dot next to 'New Referencing Outlet' and drag to the view controller file until you see the blue guiding line:

Create Outlet
Create Outlet

Provide a name for the variable that will represent our view and click 'Create':

Name the view variable
Name the view variable

We can see a new connection has been created:

Outlet created!
Outlet created!

Repeat the same for the rest of the components that require an outlet:

  • image view:  creationImageView 

  • start over button:  startOverButton

  • color label:  colorLabel

  • color swatches container:  colorsContainer 

  • share button:  shareButton

You should have the following outlet variables in your controller:

@IBOutlet weak var creationFrame: UIView!
@IBOutlet weak var creationImageView: UIImageView!
@IBOutlet weak var startOverButton: UIButton!
@IBOutlet weak var colorLabel: UILabel!
@IBOutlet weak var colorsContainer: UIView!
@IBOutlet weak var shareButton: UIButton!

Creating IBActions

We need to create actions for the 'TouchUpInside' event for the following buttons:

  • Startover

  • Colors

  • Share

We'll start with Startover button. Select the button and right click to access the context menu. This time,  select the circle against 'TouchUpInside' and drag to the controller file until the blue guidelines appear:

Create Action
Create Action

Provide a name for the function that will represent our action and click 'Create':

Name the function
Name the function

And observe a function added to the ViewController file:

@IBAction func startOver(_ sender: Any) {
        
}

Next, select the first color button and repeat the process, but this time, select UIButton for type:

Create Color button action
Create Color button action

Then, we are going to repeat the same thing for the rest of the buttons except, instead of creating an individual function for each action, we are going to connect them to the same function. Then we'll be able to tell the buttons apart by the sender parameter of that function. Drag the 'TouchUpInside' handle to the controller file hovering the applyColor function we previously created until it gets wrapped in a blue bubble:

Attach action
Attach action

Now all our color buttons are connected to the same function.

The last button to connect is "share". Go ahead and connect it on your own with the function name Share.

Then, let's add test code for each button using the print function. As a result your code should look like this:

@IBAction func startOver(_ sender: Any) {
    print("Starting over")
}
    
@IBAction func applyColor(_ sender: UIButton) {
    print("Applying color")
}
    
@IBAction func share(_ sender: Any) {
    print("Sharing")
}

Test it out, run the app in simulator, click all the buttons, and when you observe the console it should display the following output:

Startingover
Applyingcolor
Applyingcolor
Applyingcolor
Applyingcolor
Applyingcolor
Sharing

Good progress!

Connecting Model with the code

Oh, that! That's already done!

Done-done-done!
Done-done-done!

Xcode has already made our data model available in our code! The way Swift projects work is that we don't need to explicitly connect the files. The code in all files is sharable between the files. For now, let's create a variable for our creation and place the declaration right after our outlets:

var creation = Creation.init()

That didn't trigger any errors!

Done & Done!

Let's Recap!

  • To connect View with controller, we need to create variables associated with view elements and functions associated with the actions of those elements by creating Outlets and Actions

  • Model components are available by default in all controller files of the application.

Example of certificate of achievement
Example of certificate of achievement