19. Building our SUPER basic app 2/3

I bet you have just been dying to know the answer to the questions I posed at the end of our last post. In order to hook up our .xib file to the code side, how many IBOutlets and IBActions will we need?

Based on the description of the app, we need to be able to change the text content of the two labels. Do we need to change anything with the appearance of the button? That wasn't explicitly stated as a functional requirement so we're going to pass on that. So because we need to change our two labels, we will need two IBOutlets. And then because we are going to respond to one click action (from the UIButton), we will need one IBAction.

Before we dive into setting everything up, think back to a few posts ago where I asked about how we are going to keep track of what number we are on. Do you have an idea? Based on our discussions about variables, and their purposes, what could we use to keep track of our increment progress as we go through the app? Class variables! You got it. Can you see how those are going to help us? If not, you will soon. Click here if you need a refresher on class variables. So let's move on.

You should be able to hook these up on your own by now but I'll go through it anyway.

The first step to setting up the IBOutlets/IBActions is to declare them in the header file (.h file):

Can you tell what we are doing? Where is my class variable? This shouldn't look too foreign to you. Are we done? Nope! We need to implement our IBAction. So go ahead and copy the method signature and paste it into the .m file like this:

There are more methods below, I just wanted to show a basic implementation of our IBAction. Are we done yet? No! We have set everything up in code now, and we have prepped everything in IB as well, now we need to hook them together. Click here and here for a refresher on hooking them up. Basically, you just go into IB, right-click on Files Owner, and drag from the little circle that corresponds to the item you want to link to directly to the item. Try it out and don't forget to hook up the action as well!

When you save and build and run, you should see a message in the console for every time you hit the button. Don't worry, in our next post we are going to actually put some logic into our app so that it does something interesting when we press the button. make our button do something.

You're probably wondering why we're going through this again, but I promise it is necessary. Lot's of repetition makes for solid learning.

No comments:

Post a Comment