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
Before we dive in and start learning new concepts, let's revise everything we need to know up to this point. We'll get some practice with IBOutlets and IBActions and then implement some basic Auto Layout.
Teacher's Notes
-
0:00
Now that we know,
-
0:01
what we're building, let's warm up our brains with something simple.
-
0:04
We'll start by creating a brand new project and
-
0:07
playing around with a button, to change the visual elements in our app.
-
0:11
Then we'll add multiple views,
-
0:12
something that will turn out to be quite easy actually.
-
0:15
And finally, we'll add some code to transition in-between these views.
-
0:20
Open up Xcode and let's select a create a new Xcode project.
-
0:26
The last time we selected a single view application template.
-
0:31
Because we created an app, with a single view.
-
0:34
The other options offered, lets us create multi-view apps with more ease.
-
0:39
But a lot of the stuff we're going to learn about,
-
0:42
is already included in these templates for you.
-
0:45
Now once you know, what's going on, you can use these convenient templates.
-
0:50
But for now, we're going to select the single view application again and
-
0:54
hit next.
-
0:56
Let's call this app algorithm.
-
0:59
But rhythm, as in musical rhythm.
-
1:02
Now, before you make fun of me for
-
1:04
this name, our Android teacher, Ben, used to be in a band.
-
1:08
And this happens to be one of his album names.
-
1:11
Feel free to judge Ben on Twitter, at your own convenience.
-
1:15
The next two fields should be familiar.
-
1:18
My organization name is Treehouse.
-
1:20
And the identifier is com.teamtreehouse.
-
1:24
Make sure you select objective C, as the language and iPhone as your device.
-
1:30
Let's leave Use Core Data unchecked and move on.
-
1:35
Save the project somewhere convenient for you and then hit create.
-
1:40
Okay we have a fresh project, let's explore a bit.
-
1:43
We have our standard app delegate files, a view controller,
-
1:48
the main dot storyboard file, and the launch screen files.
-
1:53
Awesome!
-
1:53
Let's head over to the main dot story board file and warm up our iOS muscles.
-
1:59
Now the first thing we want to do,
-
2:01
is disable the new Xcode feature, size classes.
-
2:05
As I've said in previous projects, size classes are an important new feature of
-
2:10
iOS 8 and Xcode 6, that lets us design and build our apps on a canvas,
-
2:17
that's not tied to a specific device, but rather, applies to all devices.
-
2:22
Using size classes,
-
2:23
we can adapt our designs to different device sizes and orientations.
-
2:28
Now, the main advantage, is when we're designing apps across iPhones and iPads.
-
2:33
And since we're going to keep things simple, and target iPhones only,
-
2:37
I'm going to disable this feature and work only with the iPhone size class.
-
2:41
In the main .storyboard file,
-
2:43
pull out the utility panel by clicking on the last icon in the toolbar up here.
-
2:49
Navigate, to the first section, the file inspector.
-
2:53
And under the interface builder document section, uncheck use size classes.
-
2:59
Now make sure we keep size class data for
-
3:02
the iPhone and then hit disable size classes.
-
3:06
Now our view should be a little bit more familiar and
-
3:09
some what more like an iPhone shape.
-
3:11
Let's make some more space.
-
3:13
Okay, when we worked on the Fun Facts app last, we used a button, so
-
3:17
let's drag one onto our view from the object library in the utility panel.
-
3:22
We're familiar with buttons and so far we've learned two things that we can do.
-
3:27
We can create interface builder outlets and
-
3:30
actions, to control what goes on in our apps.
-
3:33
So, for example, we can change the text of the button and
-
3:36
the view's background color once we press the button.
-
3:39
Let's give that a try.
-
3:40
Let's get rid fo the Utility Panel to make some more space, and I'm going to
-
3:44
bring up the Assistant Editor, by clicking on the second icon in the first grouping.
-
3:51
Now, in the Jump bar on the top of the Assistant Editor,
-
3:54
we need to make sure we're in the viewcontroller.h file, the header file.
-
4:00
Click on the button, and then with your control key pressed,
-
4:03
drag it over, to the header file to create an i b outlet.
-
4:07
Now we're only warming up here, and
-
4:09
we're going to use this button to learn some general concepts.
-
4:12
So let's just call it, a button for now.
-
4:15
Normally, we're pretty strict about using descriptive names so
-
4:19
that our code is easy to read.
-
4:21
But, we're eventually going to get rid of this button, so no big deal.
-
4:25
Let's leave the type as UIButton, and Storage as Weak, and hit connect.
-
4:31
Now, we can use this button in our code.
-
4:34
Let's switch over to the implementation file.
-
4:36
Now you can do that using the jump bar up here, or a handy shortcut,
-
4:41
is once you're in the editor, click command, control, and use the up or
-
4:46
down arrows to switch between the two files.
-
4:49
Once the view loads, it calls the view did load method.
-
4:53
So let's add some code in here to change the title of the label.
-
4:58
Let's get rid of this comment, and go back to our standard editor.
-
5:02
So there's more space.
-
5:06
Let's add self dot a button, and then use
-
5:10
the method set title to programmatically set the title of the button.
-
5:15
Let's set it to, press me!
-
5:19
ForState let's do UiControlStateNormal.
-
5:26
We use the set title method on the button instance to set it's title.
-
5:30
It takes a string for the text, which we've set to PressMe!
-
5:34
And then a state for which to use this text.
-
5:37
Buttons have a few different states, which is an e-num called UI control state.
-
5:42
Normal, is its default state, that you use on your view, right now.
-
5:46
Now, some of the other states are highlighted, disabled, selected,
-
5:50
and so on.
-
5:52
We can set titles for those states, so that if our button gets disabled,
-
5:56
we can let our user know that they can't press it by changing the text.
-
6:00
Now let's head back to the main.storyboard file.
-
6:03
And everything looks okay here.
-
6:05
But if we run the app,
-
6:06
we may get some weird bugs depending on which device you run it on.
-
6:11
Now as you can see on ours, you can't even see, the entire button.
-
6:14
And that's because we haven't specified the layout.
-
6:17
Now let's get some of that practice in as well.
-
6:20
We'll keep it simple.
-
6:21
Let's hit stop to stop the app.
-
6:23
And we're going to center this button horizontally and vertically in our view.
-
6:27
So click the button.
-
6:29
And then in the Auto Layout menu, click the first icon, that is the Align icon,
-
6:36
and select Horizontal Container and Vertical Center in Container.
-
6:40
Let's make sure to update the frames for
-
6:42
items of new constraints and let's add those.
-
6:47
Cool.
-
6:47
Now, let's select a simulated device, again, to run this on.
-
6:50
I'm going to pick the iPhone 6, and hit the play button to run.
-
6:55
Once it loads, our button text should say Press me!
-
6:59
Even though the default text still says button in interface builder.
-
7:02
Using the outlet, we were able to modify our button's properties programmatically.
-
7:08
Now nothing happens, when we click on the button.
-
7:11
So let's create an IB action.
-
7:13
Again, let's go back to interface builder.
-
7:16
We're going to go into the assistant editor so
-
7:19
that we have our files side-by-side.
-
7:21
Now this time we want the implementation file, which came up correctly.
-
7:27
So let's select the button in Interface Builder and
-
7:29
Ctrl+drag to the bottom of the implementation file.
-
7:33
This should create an action by default instead of an output.
-
7:37
Let's call the action, Button Pressed.
-
7:40
We're gonna leave the type and event as it is and select none for Arguments.
-
7:45
And let's hit Connect.
-
7:47
I'm gonna make some more space here, now when we press the button,
-
7:50
we execute this method.
-
7:52
So inside this method, let's change the background color of our view.
-
7:57
We'll do self.view to get the view and
-
8:00
then background color to get the background color property.
-
8:04
Now UI color, has some handy convenience class methods for common colors.
-
8:09
So let's call the orange color method here.
-
8:11
[BLANK_AUDIO]
-
8:16
Okay. Let's stop and run the app again.
-
8:18
[BLANK_AUDIO]
-
8:20
The button says press me just as we expected and
-
8:23
when we do that, the background changes to orange.
-
8:27
Now that was all a review of what we covered in our previous course.
-
8:31
You know this stuff, right?
-
8:33
Well, that's not all buttons can do.
-
8:35
In most apps, clicking a button, often takes you to another page or
-
8:39
initiates some action, somewhere else in the app.
-
8:42
Well, let's jump to the next video to see first how we can include multiple view
-
8:47
controllers in our app and then, how we can transition between them.
You need to sign up for Treehouse in order to download course files.
Sign up