Learning Swift: Calculator App, part 3. IBOutlet and button touch highlighting

This is part 3 of my Learning Swift Calculator App.


  • Working on UIX (visual cue that a button has been touched)

Lessons learned:

Yes – there is only one item on this update.  Adding a state change when a button is touched sent me down a long rabbit hole I did not expect would end where it actually ended up.  Let me explain.

My experience is multimedia development comes largely from Adobe Flash Professional.  This means I was thinking in terms of button states, properties, and id.  Swift and objective C certainly have similar features, but I was stuck on step 1.  How do I reference this button that was created through the GUI/storyboard.  Many of the how to’s/tutorials showed how to create a button programmatically and, then, how to change the property of the button (such as the border of background color).  In Flash, I was used to just creating something, setting an ID, and being able to programmatically manipulate it.

Frustrated, I decided to look at Apple’s documentation instead and found thttps://developer.apple.com/library/IOs/documentation/UIKit/Reference/UIButton_Class/index.html) that there was one simple property that would achieve what I had set out to do – showsTouchWhenHighlighted

This was helpful, but it still didn’t answer how to refer to the button.  So how do I actually set the property for the button that was created via the GUI/Storyboard?

@IBOutletvar btn01: UIButton!

Yes, it was indeed that simple, but if you don’t know what you’re looking for, then you don’t know what to search for.  Getting this far took way longer than I expected.  So with that one line of code, I could change the property.

btn01.showsTouchWhenHighlighted = true;

Lastly, I also had to change the background color of the app so that I could see the highlight (not easy to see on the default white background)

self.view.backgroundColor = UIColor.blackColor()

Those two lines were added to the viewDidLoad function as follows:

    @IBOutlet var btn01: UIButton!

    override func viewDidLoad() {
        // Do any additional setup after loading the view, typically from a nib.
        btn01.showsTouchWhenHighlighted = true;
        self.view.backgroundColor = UIColor.blackColor()
This entry was posted in Class, Professional Development, Research and tagged , . Bookmark the permalink.

Comments are closed.