Mobile App Development / iOS App Development

Understanding Storyboards and ViewControllers

This tutorial will teach you how to use Storyboards and ViewControllers to create and manage your app's screens. You'll learn about the role of ViewControllers and how to navigate…

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Covers the fundamentals of iOS app development using Swift and Xcode.

1. Introduction

Goal

The goal of this tutorial is to provide a comprehensive understanding of Storyboards and ViewControllers in iOS development. By the end, you'll be able to create and manage your app's screens using these components.

Learning outcomes

  • Understanding what Storyboards and ViewControllers are
  • Learning how to create and manage different screens using Storyboards
  • Understanding how to navigate between different screens using segues
  • Getting familiar with the lifecycle of a ViewController

Prerequisites

  • Basic knowledge of Swift programming language
  • Familiarity with Xcode IDE

2. Step-by-Step Guide

Storyboards

A storyboard is a visual representation of the user interface of an iOS application, showing screens of content and the connections between those screens. In a storyboard, you can lay out visual components, set their properties, and create navigation flows.

ViewControllers

A ViewController manages a single content view with its associated controls and manages transitions between views. It coordinates with model objects and other controller objects.

Creating a Storyboard

Open Xcode and create a new project. You'll see a file named Main.storyboard. This is where you design your application's user interface.

Creating a ViewController

In the storyboard, click on the + button in the top right corner, search for ViewController, and drag and drop it onto your storyboard.

Connecting ViewControllers

To navigate between ViewControllers, we use segues. Ctrl+drag from a button in one ViewController to another ViewController to create a segue.

3. Code Examples

Creating a segue

// This is an action connected to a button
@IBAction func navigateToSecondScreen(_ sender: Any) {
    performSegue(withIdentifier: "SecondScreenSegue", sender: self)
}
  • performSegue(withIdentifier:sender:) is a built-in method to perform segues programmatically.
  • "SecondScreenSegue" is the identifier of the segue we want to perform. This identifier is set in the storyboard.

Preparing for a segue

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "SecondScreenSegue" {
        // getting a reference to the destination view controller
        let secondScreenViewController = segue.destination as! SecondScreenViewController

        // set properties on the destination view controller
        secondScreenViewController.someProperty = "Hello, Second Screen!"
    }
}
  • prepare(for:sender:) is a method that is called before a segue is performed.
  • We use it to pass data between ViewControllers.

4. Summary

In this tutorial, we've learned about:

  • The role of Storyboards in designing the user interface
  • The role of ViewControllers in managing screens
  • How to navigate between screens using segues

Next, you could learn more about different types of segues and how to create custom transitions between screens.

5. Practice Exercises

  1. Exercise 1: Create a new project and add three ViewControllers. Navigate between them using segues.

Solution:

This exercise should be straightforward if you followed the tutorial. Remember to give each segue a unique identifier.

  1. Exercise 2: Pass data from the first ViewController to the second.

Solution:

You can do this in prepare(for:sender:) method. Define a property in the second ViewController and set it in this method.

  1. Exercise 3: Dismiss the second ViewController programmatically and pass data back to the first ViewController.

Solution:

This can be done using delegation. Define a protocol with a method to pass data back in the second ViewController. The first ViewController should adopt this protocol. When dismissing the second ViewController, call this method.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

WHOIS Lookup Tool

Get domain and IP details with WHOIS lookup.

Use tool

Unit Converter

Convert between different measurement units.

Use tool

Open Graph Preview Tool

Preview and test Open Graph meta tags for social media.

Use tool

Hex to Decimal Converter

Convert between hexadecimal and decimal values.

Use tool

Meta Tag Analyzer

Analyze and generate meta tags for SEO.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help