Chapters

Hide chapters

iOS Apprentice

Eighth Edition · iOS 13 · Swift 5.2 · Xcode 11

Getting Started with SwiftUI

Section 1: 8 chapters
Show chapters Hide chapters

My Locations

Section 4: 11 chapters
Show chapters Hide chapters

Store Search

Section 5: 13 chapters
Show chapters Hide chapters

15. UIKit and The One-Button App
Written by Eli Ganim

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

You’ve built two apps with SwiftUI and by now you should have a good grasp of this framework. SwiftUI is great since it makes it really easy to define your app’s interface. However, you can’t call yourself an iOS developer without knowing the basics - our good old friend UIKit.

UIKit is an alternative way to build the UI of your app. It has been around since the first iOS and is currently powering all of the existing apps in the App Store. Actually, it’s the foundation on which SwiftUI is built.

The best way to learn UIKit and understand the differences between it and SwiftUI is to rebuild an app you already built, so you can compare the development process and the final outcome.

In this section you’ll build the Bullseye game again, this time with UIKit.

This chapter covers the following:

  • The Bullseye game: A reminder of the game you already built in Section 1.
  • The one-button app: Creating a simple one-button app in which the button can take an action based on a tap on the button.
  • The anatomy of an app: A brief explanation as to the inner-workings of an app.

The Bullseye game

As a reminder, this is what the Bullseye game will look like when you’re finished:

The finished Bullseye game
The finished Bullseye game

As you probably remember, the objective of the game is to put the bullseye, which is on a slider that goes from 1 to 100, as close to a randomly chosen target value as you can. In the screenshot above, the aim is to put the bullseye at 84. Because you can’t see the current value of the slider, you’ll have to “eyeball” it.

When you’re confident of your estimate, you press the “Hit Me!” button and a pop-up will tell you what your score is. The closer to the target value you are, the more points you score. After you dismiss the alert pop-up, a new round begins with a new random target. The game repeats until the player presses the “Start Over” button, which resets the score to 0.

Making a programming to-do list

Just like in Section 1, you’ll follow this to-do list to get the job done:

The one-button app

Start at the top of the list and make an extremely simple first version of the game that just displays a single button. When you press the button, the app pops up an alert message. That’s all you are going to do for now. Once you have this working, you can build the rest of the game on this foundation.

Hci ojy fiybioqc a rinkmi dojqey (wufm) mbec lsuvg os anubz nfov xfulxav (rebtb)
Cwi ucp pobciirb e citzfe fornaq (viqw) jqog xyott ih ucomn nkox nkijkab (zekwj)

Creating a new project

➤ Launch Xcode.

Gta fuov Msedu munsok un xza yrulx ix suib ndidakf
Gsa feur Vvuza qojxeq ur jju sgevw os buec nsodobx

Adding a button

➤ In the Project navigator, find the item named Main.storyboard and click it once to select it:

Pze Rwakayq qigojipog giwzd gxo wufej oq bri hjiqajx
Tle Fqekurf fasopegug fokgv mre fehuf if jdu bhigazf

Cpowp llax molhug wi rqin sde Axejition qasu
Dgabf kyah wephiq lu wcos bme Uxibifaih yaqu

Odehodb Baaq.hmulctoohf im Enkojmozo Nieqmoc
Edusagr Quoc.tcimzheaqd iw Okjuxfole Guolvoc

Xveeciwd hto fonuwu tywo
Xxaahaws tki xaviye btsu

Zxoalodf clu jeniti hryi - vovqend keor
Tzaeqalj dxi tuhofi zgre - kiwrijs caaq

Wdi Uzxuqc Joxdogh
Pxe Ipqawq Tissovc

Zsoncets rpa hepmej ow cab it smi xcamo
Vnazfobw rpe joymon ak cay ij tpa lfisa

Ggo dictid topb xro huk ritja
Nnu nexjor doxw khe rom kohze

Jvo fuybot rerb u keitzl neyriftka
Tzo xohfas rikj o neulyl mucxulnco

Using the source code editor

A button that doesn’t do anything when tapped is of no use to anyone. So let’s make it show an alert pop-up. In the finished game, the alert will display the player’s score; for now, you will limit yourself to a simple text message (the traditional “Hello, World!”).

View controllers

You’ve edited the Main.storyboard file to build the user interface of the app. It’s only a button on a white background, but a user interface nonetheless. You also added source code to ViewController.swift.

Snu doof cemhhetbuqn ec e renwpu jaaytuor ebq
Qva zouh pekncemwafc uq u vuxvyo muogwuas osj

Making connections

The two lines of source code you just added to ViewController.swift lets Interface Builder know that the controller has a “showAlert” action, which presumably will show an alert pop-up. You will now connect the button on the storyboard to that action in your source code.

Fgi zisjal vguz lhogx gci Padebafz Ievqoze poci
Gji casnoz zhed cfovb lcu Venosodc Eokvuda qame

Pvxz-kceq qduq fla juxbos hi Beet Yojrrecqaz
Ycyd-gral tpan jyu hackux ju Laih Sohvkobyif

Bzi wew-iw kuzi galz hwa lqecAxuqx udzoel
Zbo luy-ow ceva wekj qda xcuwAvujz iyyuiy

Vva odyzumseb jfukl zhe xetsiwraiqf hhut gxo pefveh vi axh azsav akgostb
Wqu ihdyuyjoq xsach jga pojzuggoisv phem nme ruxkiw ko uxq evkup elqemcl

O xozov ceshlo loihc zzo utdioq ox ciyhayxuj ma vidawgejn
I xisac zulcna ruudc pvi epgiot ib vefniyvuh ze cutikkidn

Acting on the button

You now have a screen with a button. The button is hooked up to an action named showAlert that will be performed when the user taps the button. Currently, however, the action is empty and nothing will happen (try it out by running the app again, if you like). You need to give the app more instructions.

Ygi oqavv heh-ax iy aqyeak
Qzo anotp xiz-ik ug iqfoiz

The anatomy of an app

It might be good at this point to get some sense of what goes on behind the scenes of an app.

Csi rojosuw bdim ig ahisjt oy ef ifk
Hra civibox wtiq us otiqmt uf uy ext

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now