Join Todd Perkins for an in-depth discussion in this video Exploring button states and types, part of Cocoa with Swift Essential Training.
In addition to what we've already looked at with buttons, I want to talk about button states. And the reason why I want to talk about that topic is because it can be a little bit confusing. So we'll start by dragging a push button into our view and let's look over in the state section. So the state is set to Off and if I change it to On or Mixed and I run the application, nothing will happen. It doesn't appear to do anything at all. But then I also have a state determined in the control section inside of the attributes inspector.
This sate enables me to say whether the button is enabled or not. That is to say, whether or not it receives mouse events. When I disable the button, it becomes grayed out and no longer receives mouse events. So I'll enable the button. So what does this other state do if it appears to do nothing? The reason it appears to do nothing is because this particular type of button does not support that feature. So to see the feature, let's grab a check box. So I'm going to grab a check box and if you have trouble finding an object library, you can just search for check box.
And when I hover over it, you'll notice that it is an NSButton, just like our our push button. So I'll click and drag this onto the screen and I'm going to copy and paste it. So I want a total of four buttons, so that we can look at the one kind of state versus the other kind of state. So the first button I'm going to leave as state on, which is the default, and enabled, also default. For the second checkbox, I'm going to change state to off and leave it enabled.
And you can quickly see the feedback on the screen showing that the box is then unchecked, but it is not grayed out. So this state refers to whether the box is checked or not. So now I can select this third one and leave the state at on here, but change it to disabled and you'll see that it's grayed out even though it's checked. And finally for the fourth button I will disable it and I will change the state to off, so I cannot receive mouse events for that check box and it is unchecked.
So the difference between the state and the button section and the state and the control section is that the button state refers to the button being checked or not, and the Control section refers to the button being enabled or not. Now let's also talk about button types. So I'll select this button at the top here, and we've briefly talked about the types, which controls the button behavior. Right now it says Momentary Push-In. So there's a few different options that we have to control how the button behaves. Let's look at one really quick.
I'm going to choose Momentary Change, and what Momentary Change does that's different is that when the button is highlighted, I can change the title of it to the alternate title which I can set in the alternate field. So if I type alternate here and then I run the app, I can click the button and while my mouse is down I'll see the word, Alternate. And there it is. This button could use a little more space of course, because that e is getting cut off at the end.
So if you wanted to have text that's larger than the title, then just make sure that you extend the size of the button to accommodate that text with your alternate text. And again, this might not be a feature that you will necessarily use, but the reason I'm showing it is not just so you know how to handle a Momentary Change button, but so that you know that you can change the type to suit your needs. And if you want to create different behavior for your button, you can set that right here.
- Creating your first Cocoa app
- Understanding the relationship between Cocoa and Swift
- Creating a playground
- Working with variables, functions, arrays, and loops
- Creating basic interactions and quick connections
- Building custom controller classes
- Using delegates
- Creating and arranging interface elements
- Using data controls
- Debugging and troubleshooting
- Distributing a Cocoa application
Skill Level Beginner
Q: This course was updated on 5/03/2016. What changed?
A: We added one new tutorial explaining how the Swift 2 and Xcode 7 updates affect the training in this course (which was recorded using Swift 1 and Xcode 6). The changes are minor, so Todd provides some short tips for using optionals, coding with NSDate() and NSURL(string:), and working with other smaller changes in the framework.