Chapter 4. View controllers, views, and outlets
This chapter covers
- Exploring the view controller life cycle
- Creating views
- Modifying properties of views
- Connecting views in the storyboard with code
Now that you’re familiar with Xcode and how to create a project, and you’ve explored Swift, the language you’ll use to build apps, you’re ready to start building an app.
In this chapter, you’ll use view controllers and views, the basic building blocks of building any app, to build two example apps:
- You’ll build a basic “Hello World”-style interface purely in code in an app called ViewsInCode.
- You’ll then build views into an interface in Interface Builder in a distance converter app. Using the Distance structure that you built in chapter 3, the distance converter app will convert distances from miles to kilometers.
In the next chapter, we’ll look at integrating user interaction with the distance converter app. In later chapters, we’ll look at techniques for laying out an interface. But first, we need to look a little closer at the view hierarchy.
As mentioned in chapter 1, everything you can see in your app is either a view or contained within a view. Examples of views are labels, images, or plain vanilla—views! Controls such as buttons, date pickers, and switches are types of views, too.
All the views in your app could be represented in a hierarchy—views can contain other views. Right at the top of every view hierarchy of an iOS app is a special view called the window.