Mac Programming Lesson 4
Hey guys, this is Mac. Kids in one with Mac programming lesson 4. In this programming lesson, I'm going to be showing you how to use both NSSlider and NSProgressIndicator. An NSProgressIndicator is kind of like a loading bar, so it's like a little, you know, thing that like loads whenever you're copying a file; you will see one. An NSSlider is something that you can slide back and forth, such as the volume adjustment thing that indicates a value.
So, I'll open up Xcode and I'll create a new project application, Coco application. I'll call it NSSlider and NSProgressIndicator. Okay, there we go! And so this is actually pretty easy. I'm going to, under classes, create a new file, Coco Objective-C class, and I'll call it AppController.
Okay, so this is what we're going to be using for all our link-ups in Interface Builder. We're going to have two IBOutlets: an IBOutlet for an NSProgressIndicator, and I'll call that progress1. And we're going to have an IBOutlet for an NSSlider, and I'll call that sliderSL1. And we're going to have one IBAction that is sliderValueChanged.
Okay, and I'm going to implement this sliderValueChanged right in here. So, right now, we'll have an outlet for slider1. So what I'm just going to do for now is log slider1's value. So I'm going to NSLog("%lf", slider1.doubleValue).
So the, um, a double is like an INT, except it can have decimal places. There are actually four different types of numbers: an INT, a float, a long, and a double. A long is like an INT, and the double is like a float. In this case, um, to print, using, say, NSLog, a double, you use %lf, and that will just change to be the next double parameter.
So right here, whenever we slide the slider that we're going to be hypothetically linking up, we're going to have in our console a log of what the value is. So now I'm going to edit up MainMenu.xib, and I'll call it NSSlider and NSProgressIndicator, just for fun.
And they'll drag on, you guessed it, a slider and the progress indicator. So here's a progress indicator loading bar. So on the loading bar, for now, we're going to set intermediate to be false, so we're going to uncheck that. And then the minimum value we're going to change from being 20 to being zero, and then the current value is going to be, let's make it 50.
Okay, so here we go! We have our clone is now available. So now, to link these together, I'm going to drag on a new blue object to our document window. Just get that by going to Window > Document. And on this, we're going to set the class to the AppController.
So now I'll click Control from AppController to the progress indicator and link it up with progress1. And I'll do the same with the loading bar. Okay, so now we have our interface almost hooked up.
Now I'll just click Control from the loading bar to AppController and say sliderValueChanged. So now when we build and go, I'll open up the console. Whenever I slide this slider, we'll get a very exact number in the log of what the value of the slider is. So here it's zero, here it's 100, and we can try to get as close to 50 as possible.
No possible! Oh, there we go! So as you can see, that's pretty easy to get the value. Now we're going to also, as an example here, set progress1's value. So you can set that double value. I'll set it to 50, so now progress1's value will be 50 whenever we slide the slider. So as you can see, it's 50.
So you guessed it! We can change 50 to be slider1.doubleValue, and now I'll comment out that in this log so it doesn't run anymore. These things will have the exact same value. How cool is that? So in my opinion, it's pretty cool that you can do this.
Now, wouldn't it be nice if you can make it so while you're sliding the slider, the value keeps on changing, not just when you let go? Well, it's really easy to do that, and here's where some new code is coming in. There are two ways to do it that are pretty easy.
This first way, you just set a property in Interface Builder. So if we take a look, here's the window. You just click this, and under Continuous under Control, you would just check that, and then it'll work perfectly. So as we can see now, it'll work because I checked Continuous in Interface Builder.
But there's a second way to do that, and this is a cool thing, a cool skill you can have. We're going to learn how to run code when the application first opens. The way you do this is you add a new method implementation right in here. You just do void awakeFromNib()
and Interface Builder will call awakeFromNib on every single thing everywhere.
So here's awakeFromNib, and here's code that will run the first time AppController is open. So the first time this window gets open, the second the window is opened, all this code will run. So I can do NSLog here just to make sure. I'll say "Program opened," so that way we know that this code is really running when the program opens.
So now if I run this, the code will run when the program opens. See "Program opened" right there? And so what I'm going to do in here is I'm going to set slider1.setContinuous(YES). And so this code will run at start.
So obviously, the slider will be continuous because I'm setting the property on it of continuous to YES. So if I were to comment out this code right here, it would no longer be continuous. So in my opinion, this is cool, and I really like it.
Now there's another glitch in our app that you might have noticed or not. When you drag the window around, those things stay there, and you can kind of get rid of them by doing that. So how do we make it so they resize to the window? And let's say there's a minimum size on the window.
If we click on the window right here, if we click on the progress bar of the window, we just have to single click it so we get window properties. We can set, we can check "Has Minimum Size," first of all, to set the minimum size. And I'll click "Use Current" to make it the current size.
And so now the window will not be able to get any smaller than it is now, as you can see. And what we can also do is click on, let's say, this loading bar thing, go under this little ruler, and we can set it to stretch right there.
And now it'll stretch out, and we can do the same exact thing for here. So now these objects will spread out, and there's a minimum size. So this is how to do some cool windows stuff, how to use sliders, progress indicators, and how to run code when the application first opens.
So I hope you learned something today; I know I did! So thanks for watching MacHeads in One. Please ask any questions and comments, um, write in the comment box or by personal message. Subscribe and goodbye!