Is your button printing to the console? I hope so. Let's move on and get that button to do something useful. We talked about the behavior that we wanted the button to accomplish. We press the button, and the number increments by one and displays a message based on that number. If you think about it, there are many different ways that we could accomplish this. For our purposes, we'll start out with the most basic I can think of: the if statement.
The purpose of the if statement is pretty simple. We use it to do a test on the result of an equation. What equation? Well that ties in with our discussion about logic. Consider this equation:
1 + 1 = 3
Does this equation evaluate to true or false? In other words, is the above equation right or wrong? It's wrong or false. That is easy. The important thing to realize is that we can do tests (mathematical or Boolean) to see if certain conditions exist, and we can act accordingly.
Lets think of an example of how we could use an if statement. When the user of our app first presses the button, and before we increment any variable values, what is the value of the counter variable? Well, we haven't set it to anything yet so it will default to 0. So the purpose of the counter variable is to allow us to conditionally display text to the user, right? We want to show different messages for different values. So let's add an if statement to our IBAction that handles our first case:
So this is pretty simple. As I mentioned before, this is basically the most simple way I could think to do this. That's okay thought because it exposes us to some more useful concepts.
If our count variable is == 0 then control enters the scope of the if statement and executes whatever is inside. It is important to understand that we don't use one = sign because that is the same as setting count = 0 which we don't want. We want to check if the logical operation of comparing the two values results in true or false. If true, enter the if statement, else don't.
What is this count++ wizardry you are probably wondering? Well, that is a shorthand way of incrementing a numbers numeric value. It is the same as saying count = count + 1 or in other words take whatever count is and add one to it.
Based on what we have so far, how many times do you think we could press our button and have that log message printed? Answer: one time! Because after it increments after the first pass, it is never going to be 0 again. It just keeps growing with every press.
So that solves part of our problem, now what about setting the number value to the appropriate label on our screen? Or what about setting a custom message? Think about that for a bit. Where would we put code that could handle that? If you said 'within the scope of the if statement duh!' you are totally right. When we enter the scope of that if statement, we want to update the values of our IBOutlets so that they can reflect the changes appropriately.
We've gone over this before, but do you remember how we can set the value of the UILabel objects? Think about it...
Ok, this is what you do:
By accessing the text property of the UILabel objects named messageLabel and counterLabel (remember we made those IBOutlets in the .h file (if you forget what you named a variable it is super easy and convenient to press ctrl-option-up arrow to toggle between the .h/.m file!)) we can change their values by passing a variable or object of the appropriate type.
Setting the value of the messageLabel was easy because the label needs a string, and the message we wanted to send it was a string! A perfect fit. But as you see, setting the counterLabel text was a little more difficult. The label is expecting a string and the counter variable that we are trying to send it is of type int, right? We will get an error if we try and send an int to a place where a string is expected. That is why I am using the nifty converter that NSString gives us. Similar to how NSLog works, we are basically converting our int into a string so that there aren't any problems. I highly suggest you become very familiar with the name of that method: [NSString stringWithFormat: ]; You will use it ALL THE TIME.
So now that we have that done, go ahead and run the project and you will see something happen when you press the button! That's pretty neat!
So what if we wanted to handle more cases? Well, keep going! You can setup else if statements to carry on through some more testing. So if the first condition fails, it will continue on to the next:
Go ahead and fill out the if-else if statement so that it can appropriately handle 10 button clicks! Fill in the messageLabel with whatever you want but make sure that the counterLabel is accurate.
For a bonus feature, try adding a decrement button! The syntax for decrementing is what you might expect: count--; Try hooking up a decrement button so that you can go up and down through your messages but so that you can't exceed 10 and you can't drop below 0. Just try it out! It will be fun!
No comments:
Post a Comment