Storyboard for iOS Development

Storyboards were first introduced to developers in the release of iOS 5. They save developers the trouble of designing each screen’s interface in a different file. The storyboard allows you to see the conceptual mockup of your app as a whole and the interactions between each screen. Using segues, you can set how the app will transition between given screens and pass data along. In this tutorial, I will show you how to create a simple login screen for an app.

Getting Started

The first thing you will want to do is create a new project. You should select a new single-screen application, set the language to Swift, and choose universal devices. Upon navigating to the storyboard, you will see an empty view controller scene. The arrow on the left side of the view controller indicates that it serves as the root controller.

storyboard

 

In order to add labels for the user name and password, you need to go to the box in the bottom right of your screen, select the circle with a square in it, and drag and drop two labels onto your view. By choosing preferences in the sidebar, you can specify many important parameters. There are too many to go over, but most are self-explanatory.

storyboard2

The two important elements for us are the placeholder text and the secure text entry checkbox. We want to use placeholder text to inform the user about the required information and the secure entry to hide the user’s password from being displayed. Once this is done, we need a button to trigger our segue. We can drag one in the same way we did with a text field. After giving the button a title and arranging your objects on the screen, it should look like this:

storyboard3

Now our first screen of the app is done, so we need to create another one to transition to. Accomplish this by dragging a second view controller onto the screen. In order to connect the two screens, you must perform two actions. First, you will need to control-click on the button and drag it to the new screen you added. Next, you will need to select the circle between the two views. You will then need to change the identifier for the segue; in this example, we will use “nextScreen.

storyboard4

Lastly, we will put a label on the new screen so we can view the user and see if our app works. In order to access the fields from the storyboard programmatically, we need to create outlets for them in the class that uses them, as follows:

class FirstScreen: UIViewController {

@IBOutlet weak var username: UITextField!
@IBAction func loginButton(sender: AnyObject) {
perfromSegueWithIdentifier(“nextScreen”, sender: self)
}
}

class SecondScreen: UIViewController {
var user: String!
@IBOutlet weak var username: UILabel!

Override func viewDidLoad() {.                                                                                                                    username.text = user
}
}

After completing this task, make sure to link the objects from the storyboard using the connections inspector for each screen. Your outlets should appear, and you can click the circle to the right of them to establish the links.

storyboard5
The last function to be implemented is the ‘prepareForSegue’ function in the first screen’s class. This allows you to pass data to the new view as follows:

override fun prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == “nextScreen” {
let destVC = segue.destinationViewController as UIViewController
destVC.user = self.username
}
}

Now, you have a basic understanding of how to change screens and pass data between them. This will give you a good start on making functional apps. Happy programming!

ABOUT THE AUTHOR

Kevin Arrows


Kevin Arrows is a highly experienced and knowledgeable technology specialist with over a decade of industry experience. He holds a Microsoft Certified Technology Specialist (MCTS) certification and has a deep passion for staying up-to-date on the latest tech developments. Kevin has written extensively on a wide range of tech-related topics, showcasing his expertise and knowledge in areas such as software development, cybersecurity, and cloud computing. His contributions to the tech field have been widely recognized and respected by his peers, and he is highly regarded for his ability to explain complex technical concepts in a clear and concise manner.