Objective-C iPhone Programming Lesson 17 - Multiple Buttons, Shared Velocity
Hey guys, this is Mac has 101 with our 17th iPhone programming tutorial. In this video, we're going to be starting out on making multiple buttons be able to be on the screen at once. Uh, so let's get started.
Right now, what we have in our .h is a variable for one button, a single button. Now in this tutorial alone, we're going to be making it two. There are two buttons, and they'll share our velocity, and you'll see what that is later. But basically, we're not going to do it like this. We're not going to copy this button variable, call it clickme one, go into here, copy all this code, change all the click mes to clickme one, then go here and change, copy all this code, and change all the click Mees to click me one, and change the click Mees here to click me one and copy and paste and blah blah blah.
What we are going to do is have an array. An array in, uh, math and in programming at least can contain zero, one, or more than one object. What that is to say that where there's a variable that can only contain one value, an array can have any number of values, and it can tell you how many values are in it. So for instance, and we're going to be not, uh, denoting the array as brackets.
So object one, object two, object three, dot dot dot—this is how an array is represented in text; there are brackets around all the objects that are separated by commas. So in this case, we're going to have an array with button one and button two. This is what it's going to look like. In order to actually declare this array as a variable, we say NS mutable array buttons.
Now, our ultimate goal is to put two buttons into this array and use it. As you can see, we got rid of our Singleton button variable because this array will be taking its place. Another thing we're going to be declaring here is a UI button method. It'll return a UI button called create new button Pam, and let's just go in here, and I'll put this overviewed load create new button.
Now, what create new button is going to do is it's going to make a button at a random point. So let's just copy this code, paste it, since clickme is no longer a global variable, we have to declare it here, and we have to return it since this method is supposed to return a UI button.
All right, so what this method will do is it will create a button, give it the coordinates 10, 10, size 100, 100, add it to our view, make it so when it's clicked, it calls the button click method, and then it'll return that. So let's get rid of this code here; we don't need that. Now, we're going to say, uh, let me talk about one thing first.
Buttons right now now equals nil, which means nothing. An empty array is actually a left bracket, right bracket, as our notation will show. So right now, it's nothing; it can never have any values because it is empty. It's more than empty; it doesn't exist at all. If we want to make it an empty array, we have to say buttons equals NS mutable array alloc and init, and now buttons will equal this. It'll equal left bracket, right bracket.
Now we want to add two buttons to this. So first, let's get our two buttons. Button one equals self create new button, and UI button button 2 equals self create new button, and we'll say buttons add object button one, and buttons add object button two. Right now, buttons will equal button one, button two. Boom! That's what we want as we go back here and review. Oh, I got rid of it in the header, but this is what we want. So now buttons has two objects in it that are two different buttons.
Now there's a way to do this without having to declare these two buttons. I'm going to show that to you in the next tutorial, but for now, we're leaving it this way because it's easier for you guys to understand. Now in the timer tick, what it's supposed to do is move click me, which was our original button, but now there are multiple buttons, so in the ideal world, there is a way to run this code for each different button that are in the array and to have click me be that button.
And so this is really easy to do. We have a, uh, basically a flow control function in C called for, and we can say for UI button click me in buttons. Now, since this is inside of a for loop, we have to indent it by selecting it and pressing Command right bracket or, sorry, right bracket, and that'll indent it. So what this does is it will go through each button in buttons, give click me the value of that button, and then run this code where click me equals that button.
So first, it will run this code and click me will be button one. Then it'll run this code and click me will be button two. If there were three objects, it would run this code once more, and click me would equal button three. All right, pretty simple.
Now let's go down to our button click method. Basically, this method, um, right now, we just assume that click me was clicked, but at this point, we have different buttons that will both call the button click method when they're clicked. So in order to have a different behavior here and not have to reference click May because click me doesn't even exist—we got rid of it from the header—we have to say UI button sender because sender will always equal the button that was clicked when that called this method.
So let's change the declaration. Now instead of taking an ID, we'll take a UI button, and now let's replace click me with sender. That's right; it's that easy, and now it'll randomize sender's coordinates and reset sender.
All right, now before we run this, I want to go over one more thing. When one of the buttons hits the wall—let's say it hits the right wall—the x velocity will switch, but the x velocity will switch for both buttons. So if one button is going is not touching the wall, but the other one is, it will switch velocity as well. We're going to be fixing this in the next tutorial, but not in this one.
All right, so now let's go up and have it randomize the button's coordinates. So I'm just going to copy this code basically, and let's go up here, place sender with click me, and say click me that frame equals block. All right, this will work, and this will randomize the button's coordinates.
Now let's go ahead and run it. Make sure it's running on the iPhone simulator. All right, and there we go, two different buttons. When one of them hits the wall, the other one moves. You see when this one touches the top, this one goes down as well, even though it didn't touch anything.
So in the next tutorial, we're going to be making these buttons basically bounce independently of one another. Um, when you click it, it still randomizes one of the button's coordinates, not the other one. Very, very nice. But yeah, so in the next tutorial, you can expect to see we're going to have it so it randomizes or so that each of these buttons bounce independently from one another. Uhoh, okay, so we also have to fix that; we'll see how that goes.
But, um, anyway, so that is the, uh, the beginning of us having multiple buttons. You probably just saw where all the buttons flew off the side as a result of the fact that the, uh, buttons are not bouncing independently. So we really need to get that done in the next tutorial.
So what I also said would be something that I started doing in these, uh, tutorials that someone suggested I do in a personal message. Uh, what I'm going to start doing is having little challenges at the end of each iPhone programming video that I make. So the challenge for this video will be to—I want you guys to try to make it so that there are three buttons. Shouldn't be that difficult because I already showed you how to do two buttons.
Um, and leave a video response if you're having trouble or anything like that, or if you've done it successfully and you're proud of yourself. So yeah, try to get that challenge done so that there are three buttons, and in the next tutorial, I'll show you how to do that, as well as making it so the buttons actually bounce independently, and everything works perfectly.
So thanks for watching, Mads 101. Subscribe and goodbye.