Unit 17: Actions & Outlets Flashcards
Action
connects a method in source code and a control in Interface Builder
the connects allows particular code to run when a user interacts with the app’s controls
e.g., a certain method may be associated with an action, such as a button tap or a switch change
CGFloat
a type of number that can have a decimal point, such as 3.5, 7.0, or -5.5; interacts with UIColor (a common type)
Enabled
a button or control is available for interaction
Disabled
the user can still see the UI element, but touching or otherwise trying to activate the control will yield no effect on the app; appears slightly dimmed
Outlet
connects a variable in source code to an object in the storyboard; the connection allows the code to access those objects and get information or make changes when the app is running
Slider
a type of control in the UI that enables the user to select a single value between a minimum and maximum number
Switch
control that acts like an on/off button (like Airplane Mode or Bluetooth in the Settings app
There are two kinds of connections between storyboards and code:
Outlets connect variables in your code to objects in the storyboard, so you can access those objects within your code and get information or make changes when the app is running.
Actions connect controls like switches and buttons to methods in your code, so tapping a button, for example, will run a particular method.
Creating Outlets
When you develop an app, you want your code to refer to objects in a storyboard. To do this, you must make connections, called outlets, between the two.
In this part, you’ll add a view to the user interface, define and connect an outlet that connects to the view, and use the outlet you defined to set the background color of the view in code. When you’re done, the app will look like the image to the left.
Follow these steps to get started
Create a new Xcode project using the Single View Application template:
“1. Name the app “ColorMix” and fill in the rest of the details:
“2. Using the project navigator, open Main.storyboard.
“3. Find a view in the Object library. Remember you can use the Filter field to narrow down the list of options.
“4. Drag the view onto the View Controller Scene. Use the guides to align it to the top of the scene and center it horizontally.
5. A white view on a white background is hard to see. Choose Editor > Canvas > Show Bounds Rectangles to display an outline of everything on the scene.
6. Open the assistant editor by going to View > Assistant Editor > Show Assistant Editor. If you can’t see ViewController.swift in the assistant editor, make sure the jump bar at the top of the assistant editor looks
like this:
Control-drag (hold down the Control key, then click and drag with the mouse or trackpad) from the view into the code.
Enter “colorView” in the Name field and click Connect
Here’s what you’re seeing, from left to right:
Circles: The filled circle indicates that the outlet is connected. If the outlet wasn’t connected, it would be an empty circle.
@IBOutlet weak: This signals to Xcode that the property on this line is an outlet.
var colorView: This is the declaration of a property you’re already familiar with.
: UIView!: The type of the property is UIView!. The exclamation point means that if the outlet is not connected and you try to access this property, your app will crash. UIView is the basic view type used in all iOS apps. Almost everything you see on the screen is a kind of UIView, which
is responsible for drawing and handling touches.
The important thing to remember is that when you use colorView in the ViewController.swift code, you’re referring to the view you’ve just added to your storyboard.
Find the function viewDidLoad(). As you learned when printing to the console from the SinglePhoto app, this function is called when your view controller is ready to appear on the screen. Add the following code inside the function:
colorView.backgroundColor = .black
When the items from the storyboard have all been created and the outlets and actions have all been connected…
…viewDidLoad() is called. The code above sets the background color of the view to black. The view you see in the storyboard doesn’t change color, because the code isn’t run until the
app is run.
In the device menu, choose the iPhone 6s simulator, then build and run the app. You’ll see a black rectangle at the top of the screen.
Disconnected Outlet or Action—Fixing a Crash
Sometimes you’ll build and run your app with no errors, but the app immediately crashes on launch. Your device (or the simulator) shows your Home screen with an error in the console, like this:
“*** Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key someNameFromYourApp”
The error message doesn’t tell you that the problem actually stems from a view in Interface Builder, but that’s exactly where you need to look.
Open the storyboard and select View Controller…
…either in the document editor or by selecting the yellow circle at the top of the main editor in Interface Builder.
Now open the Connections inspector on the right side of the screen. The Connections inspector lists all the outlet and action connections for the selected view controller.
If you look carefully at the list of outlets, you’ll notice that most connections have either an empty circle or a filled-in circle, just like the circles you see in the gutter of a Swift file with IBOutlets or IBActions. One connection, though, displays an exclamation point instead—and that’s the source of the crash.
Imagine that in this case…
…you created an outlet from Interface Builder to the Swift code file, but entered bleuSlider as its name instead of blueSlider. After creating the outlet, you noticed the typo, fixed it, and made sure the slider was hooked up to the new name.
Interface Builder project
But that adjustment won’t fix the problem. You need to go back to Interface Builder to inform the storyboard about the outlet’s new name. Without that step, the mismatch won’t be discovered until the app launches and tries to hook up all its connections—at which point, the storyboard file will try to open a connection to a name that no longer exists. And your app will crash.
You can remove invalid connections by clicking the “x” next to the element’s name. (You can remove valid connections this way, too.) Once you’ve removed all invalid connections, this mysterious error will no longer crash your app on launch.