The next topic we are going to tackle falls right in line with the direction we've been heading. In our last post, we were able to hook up a button to a method in our code and when we clicked the button, it did something! Pretty awesome. But, what did it do? It just printed out a dinky message to the console, right? I'm afraid that's not very awesome.
After all of the ground work that we have been laying so far, we are finally in a position to start writing some actual code. Or in other words, we need to put some logic into our app.
What comes to mind when you think of the word 'logic'? Some nerd at school who was always logical? Not exactly. In programming, the term logic is used to describe the process of mentally solving a problem and then representing that solution in code.
So in order to introduce this concept more clearly, let's make an app! This may not always hold true, but I am going to suggest that the first step to writing an app is having an idea. So let's decide on an idea. I want to further enforce the importance of the last two posts about IBOutlets and IBActions. Let's make an app where we have a label that displays a number. We will also have another label that displays a different message based on the number that is displayed. And then we will have a button that we will press in order to increment the value of the number label which will in turn change the message label.
What do you think? Sound pretty awesome? I'd say it is a great next step. So if you think about it, what's next? What type of logic do we need to come up with in order to accomplish this simple task?
Before we write one line of code, we need to have a firm understanding of what we want to do. This results in much more efficient app development. Plan and think first and then make it happen. So let's talk about this. We know that with an IBAction, we can hook up a button to run a specific bit of code. Ok, once we have that happening, we need to make sure that the code that is run makes the value of the label change. But what does it need to change to? How do we know what the label's value is? How do we know what to change it to? How do we make sure that we are just incrementing it by one?
These are all problems we need to solve. We need to make sure that the logic we put into our app can handle all of the different scenarios we can see it needing to handle in order to be the app that we set out to design it to be.
Before we get too far into the nitty-gritty, let's see if you can lay the foundation of our app on your own. Based on the specs of the app I described, you'll need a button, two labels, and a way to have those all play nice together. Go ahead and try setting everything up! Hint: remember that IB is your friend!
Bonus question: How will we handle keeping track of the value of the label we are incrementing?
We're going to cover the actual logic we need in the next post, but I really want you to review what we have learned so far by taking on this little challenge.