Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Playlist Browser with Swift Working With Multiple View Controllers A Recap with UIButton

Matthias Seeber
Matthias Seeber
14,167 Points

setTitle Method Button

Can anybody help me with this method? whats wrong with it?

ViewController.swift
class ViewController: UIViewController {

    @IBOutlet weak var timerButton: UIButton!
    var redColor: UIColor = UIColor.redColor()

    override func viewDidLoad() {
        super.viewDidLoad()

        //Add your code below
        func setTitle(_ title: "Start"?, forState state: timerButton)
    }

}

2 Answers

Dominic Bryan
Dominic Bryan
14,452 Points

I am assuming you need to set the title of the button to "Start",

timerButton.setTitle("Start", forState: .Normal)

Thank you Steve Hunter who posted this response on another post a few hours back

Dom

Matthias Seeber
Matthias Seeber
14,167 Points

oh i see what does the ".Normal" mean?

Dominic Bryan
Dominic Bryan
14,452 Points

Matthias Seeber .Normal refers to the state in which the button is to set the title, so in this instance when the button is normal (i.e nothing going on) the title is "Start".

You can choose to set titles for different states, so .Highlighted for example. If you do not specify any other state the button will always use the .Normal state title as its default title no matter what, if its highlighted, selected or what ever.

Take a look at UIButton class reference setTitle(_:forState:) in apple docs: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIButton_Class/index.html#//apple_ref/occ/instm/UIButton/setTitle:forState:

and UIControlState at: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIControl_Class/index.html#//apple_ref/c/tdef/UIControlState

Just taking a brief look will give you an idea of what the state may be used for in another case (this is funny because the ControlStates are stored in a Enum.... case..... bad joke but hey ho).

Hope this helps Thanks Dom

Matthias Seeber
Matthias Seeber
14,167 Points

Thanks a lot, it was very helpful. Awesome :D