Tim Dietrich

Custom Software Developer

Home Services Portfolio Blog About Contact Newsletter

Swift: Fun With Buttons

I was working on a personal project over the weekend - a neat iPhone app to help my son study for a test that he will be taking in a few months. Like most iOS projects that I've worked on, I found myself needing to do something that I had never done before.

The app displays randonly generated "order of operations" math problems, and what makes it interesting is that it can display the problems in two ways. It can display an equation so that the user needs to determine what the equation works out to. Or it can show the numbers that make up the equation and what they work out to, so that the user has to determine what the operators are and what order they need to be in. There's a button on the view that let's the user toggle between these two options.

So I needed to add code to the view controller to support this feature. Setting up the IBAction so that the question was presented differently was fairly easy. I'm using a UIWebView to display the question, so it was a simple matter of swapping its contents using loadHTMLString.

But I also needed to change the button's title - and I had never needed to do that before. It's not quite as easy as doing something like button.title = "New Title," but it isn't terribly complicated either.

So I thought I'd write a little about the UIButton class and how you can work with it programmatically.

Setting A Button's Title

As I mentioned above, to change a button's title, you cannot simply reference the title itself. Instead, you have to use the UIButton "setTitle" method. For example...


theButton.setTitle("Clicked.", forState: UIControlState.Normal)

Notice that when calling setTitle you have to specify both the title and the state for which that title is to be applied. Here we're setting the title for the button's "normal" state, but you could also specify a title for additional states. Click here to learn about the other control states.

Getting The Button's Title

You might run into a situation where you want to know what a button's current title is. There's a method for that as well, and it's "titleForState." In this example, I'm using titleForState to print a button's current title.


print("Button title: \(theButton.titleForState(UIControlState.Normal)).")

Like the setTitle method described above, titleForState also requires that you specify the control state that you want to get the button's title for.

Changing A Button's Title Color

Suppose that you want to change a button's title - maybe make it appear to be dim under certain conditions. You can do that with the setTitleColor method. For example:


theButton.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)

You can easily specify the color using UIColor, and in this example it is being set to grey. And like the other methods that we've looked at, setTitleColor requires that you specify the control state that the title color should be set for.

Disabling A Button

You might also want to disable a button at times, and that's very easy to do. Here's an example:


theButton.userInteractionEnabled = false

That will prevent the button from being clickable. You can enable a button by setting the userInteractionEnabled property to true.

Hiding A Button

You can completely hide a button with the "hidden" property, like this:


theButton.hidden = true

An Example

So let's put some of these methods and properties to work. Here's a simple view controller that will allow a button to be clicked only once, after which its title will be changed, it will be dimmed, and disabled.


class ViewController: UIViewController {
    
    @IBOutlet var theButton: UIButton!
    
    @IBAction func buttonPressed(sender: AnyObject) {
        
        // Change the button's title.
        theButton.setTitle("Clicked.", forState: UIControlState.Normal)

        // Change the color of the title.
        theButton.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        
        // Disable the button.
        theButton.userInteractionEnabled = false
        
    }

}

Here's a very basic XCode project that shows the view controller in action. Enjoy!