Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
We used IBActions to execute a function when the button is pressed but that isn’t the only thing we can do. In this video let’s take a look at how we can access and manipulate elements in our view using IBOutlets.
Teacher's Notes:
-
0:00
When we press the button, we want to change the text in our label.
-
0:05
So, just like with the button before it, we need to hook this label up to our code.
-
0:10
Now, we can click and
-
0:11
drag just like we did with the button, but there's a more specific way to do it.
-
0:16
In the assistant editor, bring up the viewcontroller.h file, the header.
-
0:21
Right-click the label, and a modal pops up with a bunch of options.
-
0:27
We want to make a new referencing outlet to this label.
-
0:31
So let's click the plus button from the referencing outlet section, and
-
0:36
drag it to the ViewController header in between the interface and end sections.
-
0:41
Now, the last time we created an action.
-
0:44
This time, we're going to create an outlet instead.
-
0:47
Let's call this label the funFactLabel, and click Connect to create the reference.
-
0:55
Let's make some space by getting rid of the debug area.
-
0:58
Now, we just created an IBOutlet as a property.
-
1:03
When we create it as a property, we can use it in our
-
1:06
implementation file to refer to the label that we created in Interface Builder.
-
1:12
If you're not too familiar with Objective-C properties at this point,
-
1:17
you should take our Objective-C basics content to learn
-
1:19
the language before continuing this course.
-
1:22
Now, the only thing different about our property declaration here than what
-
1:26
we've seen before is this IBOutlet keyword.
-
1:30
This type qualifier, IBOutlet, is a tag applied to property declarations so
-
1:36
that, again, Interface Builder knows to recognize the property as an outlet, and
-
1:42
synchronize the display of it and the connection of it in our code.
-
1:46
Right-clicking on Interface Builder objects to create outlets and actions
-
1:51
is a more specific way of selecting what connections we want to make.
-
1:56
If you right-click on the button, you'll see that when we Control-dragged,
-
2:00
we automatically chose a Touch Up Inside type of connection.
-
2:05
Now, we can choose exactly what other type of connection we want to
-
2:09
initiate the action.
-
2:10
The most common one, and the one that we used, is the Touch Up Inside.
-
2:14
Now that we have both the label and button hooked up, let's use them.
-
2:19
When we click this button, we want to change this label's text.
-
2:23
Right now, clicking on this button executes the showFunFact method
-
2:28
in our implementation file.
-
2:30
Let's modify the contents of this label inside of
-
2:34
our method to change our label text.
-
2:36
Now, we just declared the label as a property of the view
-
2:40
control in the header file.
-
2:41
To access this property inside of our implementation file, we need to
-
2:45
refer to the current instance of the view controller using the keyword self.
-
2:51
The label is a property of the view controller so
-
2:53
we can use self along with the dot syntax to access it.
-
2:58
Let's get rid of this NSLog statement, and instead, let's write self.funFactLabel,
-
3:06
and we'll use the text property to assign a string to it.
-
3:10
We're gonna say Another interesting fact.
-
3:15
Just like the label was a property of the view controller,
-
3:19
the label has its own set of properties, one of which is text.
-
3:23
We can assign a string to text to change what we see on our screen.
-
3:28
So now, if everything works as planned, when we press the button, our label should
-
3:33
change from An interesting fact goes here to Another interesting fact.
-
3:39
And look at that, it works.
You need to sign up for Treehouse in order to download course files.
Sign up