Join Simon Allardice for an in-depth discussion in this video Exploring button states and types, part of Cocoa Essential Training.
We have seen that buttons can be enabled, or they can be disabled. If they're enabled, we can click on them. If they're disabled, we can't. But buttons also have something called State. And it's an easy beginner mistake to confuse the state of a button with whether that button is enabled or not. And State and Enabled in Cocoa, are two very different things. So I have just created a brand-new Cocoa application, and I'll jump onto the window in my MainMenu.xib, and drag on a regular push button here. Now if I look in the Attributes Inspector for this button, Apple won't really helping me understand the difference here because I do have this dropdown option called State here.
But if I go down a little bit further to find where I have got the check box for Enabled. It's in a section called State, and these are completely different from each other. So let me make this a little clearer. Enabled is simple, it's the easy one. All buttons care about whether they're enabled or not. The value of this can be changed in interface builder. I can uncheck it, or I can do it in code with the Set Enabled method. If it's enabled, I can click on it, if it's not, I can't. It's simply true or false. We're seeing as this is Objective-C, it's yes or no, but State is different.
State--I'll go back up to the top of the Attributes Inspector to find it here. State is a property that can be officially On or Off or Mixed. And this has nothing to do with whether this button is enabled or not. But here is the thing, with many of the buttons that you might drag on, State doesn't matter. You start changing the State of a regular push button like this one, you'll see no visual impact whatsoever. So what's the difference? Well, here is what I consider the best example of the difference between State and Enabled.
I'm going to go into my Object Library here and drag on a check box, and just copy and paste that. Do it a couple more times so that I have four. And yes, in Cocoa, a check box is not some special kind of class, a check box is an NSButton. That is its type, if I click over there and hover, you will see a check box is an NSButton class.
So I'm going to change the properties of three of these. I'll leave the first one as it is. Take the second one, and change its State from On to Off. Take the third one, leave its State as it was, which is On, but uncheck Enabled. Check the fourth one, I want to uncheck Enabled, and change its State to Off. And that's the difference when we're looking at this. The first two are enabled, the second two are not.
But the first one is enabled with the State of On, the second one is enabled with the State of Off, third one not enabled and On, fourth one not enabled and Off. So state is a property that does exist for every button in Cocoa but only matters for certain types of button. Because in Cocoa buttons don't just have a style, they have a Type. The style is the way they look, the Push button, Check, Square, Disclosure Triangle, but the Type, which is also available as a dropdown here, is the way they behave.
Often these two are very closely linked, but they're not the same thing. For example, a regular Push button has a certain appearance, a certain Style, but it also has a particular behavior, a specific behavior when pressed. You click it, it is momentary. It's got the Type of Momentary Push In. Now I can go ahead and run this application or another quick way I can do this is go to my Editor menu and just say Simulate Document, which is a quicker way of just taking a look at this screen without worrying about compiling code.
button behavior is momentary push in. I click it, it turns blue for a second and returns to the way that it was a moment ago. On the other hand a check box, which is still a button, has a different kind of behavior. I check it, it changes its State, it moves from Off to On or On to Off, quitting the simulator, go back here. So selecting a check box we have a different type of Type switch. If you drag down a button like a Recessed button, then you would find a slightly different behavior there, which is the Push On Push Off behavior.
But typically you don't have to worry about the Type because buttons dragged from the library have the Type that is most usual for that Style. But it can mess you up if, for example, you decided to have a check box and then change its Visual Style to Push button but leave its Type as Switch, that would be a nonstandard use of an interface control. But there are the occasional times you want to change the type of a button, here's an example. I'm going to select this regular Push button here, which by default has a Type of Momentary Push In, and I'm going to change that to Momentary Change.
What's the difference here? Momentary Change allows us to put in an alternate title so that while it's being pressed, it actually shows something different. So I'll just change this to the word, Pressed, and then go back to the Editor menu and say, Simulate Document again. I could just run the application, and we'll do the same thing. Now when we click the button, we get the alternate state of that, this is the momentary change of the button. And occasionally that kind of thing might be useful, but most of the time you drag on a button with the right kind of Style from the Object Library, you will automatically have the right type.
- Installing the tools
- Creating your first app
- Adding basic interactions
- Understanding the Cocoa application life cycle
- Creating custom controller classes
- Creating alerts
- Understanding delegation
- Working with buttons, text fields, sliders, and more
- Using layout and data views
- Adding and editing toolbars
- Using key-value coding
- Binding objects
- Debugging code
- Distributing an application
- Creating icons and full-screen apps