Jude Molloy
Jude Molloy

Reputation: 193

Swift 3 changing the title of a UIButton

I have been looking around the web and I am unable to find a solution for my problem. I want my "START" button text to change to "RESTART" once the game has ended. If any additional information is needed please let me know. Thanks, all help is appreciated. Below is my code.

class ViewController: UIViewController {

var gameOver = false
var stopFuncs = false

var gameTimer = Timer()
var counter = 10

var selected = "NONE"
var score = 0
var colour = "NONE"


@IBOutlet weak var colourLabel: UILabel!

let colourProvider = ColourProvider()

@IBOutlet weak var scoreLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    colour = colourProvider.randomColour()
    print(colour)
    colourLabel.text = colour


    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBOutlet weak var counterLabel: UILabel!

func updateCounter() {
    if counter >= 0 {
        counterLabel.text = String(counter)
        counter -= 1
    }
}

@IBAction func startButton() {
    gameOver = false
    counter = 10
    stopFuncs = false
    var _ = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateCounter), userInfo: nil, repeats: true)
}


@IBAction func greenButton() {
    if stopFuncs { return }
    compareColours(selectedColour: "GREEN")
    changeColourLabel()
    colourLabel.textColor = UIColor.green
}

@IBAction func blueButton() {
    if stopFuncs { return }
    compareColours(selectedColour: "BLUE")
    changeColourLabel()
    colourLabel.textColor = UIColor.blue
}

@IBAction func yellowButton() {
    if stopFuncs { return }
    compareColours(selectedColour: "YELLOW")
    changeColourLabel()
    colourLabel.textColor = UIColor.yellow
}

@IBAction func pinkButton() {
    if stopFuncs { return }
    compareColours(selectedColour: "GREEN")
    changeColourLabel()
    colourLabel.textColor = UIColor.red
}

func compareColours(selectedColour: String) {
    if gameOver == false && counter >= 1 {
        if selectedColour == colour {
            score += 1
            let scoreString = String(score)
            scoreLabel.text = scoreString
        }
        else {
            gameOver = true
        }
    }
    else {
        stopFuncs = true


    }
}

func changeColourLabel() {
    colour = colourProvider.randomColour()
    colourLabel.text = colour
    print(colour)
}

}

Upvotes: 2

Views: 6899

Answers (2)

Duncan C
Duncan C

Reputation: 131408

You already have IBOutlets to some of your labels.

For buttons you may need to set up both an IBaction and an IBOutlet.

An IBOutlet is a thing. A noun. It's a reference to a view object.

An IBAction is a verb. An action. It's code that gets called when something happens to a UIControl (tapping a button, moving a slider, changing the selected value on a segmented control, etc.)

The IBaction is a function that gets called when the user interacts with a control (For buttons you almost always link the action to the .touchUpInside control event.)

Naming your actions with names like pinkButton is a bad idea, and leads to confusion. I suggest renaming your actions to verbs, like handlePinkButton (or perhaps "pinkButtonTapped", but I prefer action names to be verbs.)

Note that when you rename an IBAction and/or IBOutlet you have to go into IB (IB = Interface Builder), select the connections inspector, remove the now-incorrect IBOutlet/IBAction and control-drag from the object to the updated outlet/action.

If you don't do that you'll get very confusing crashes at runtime:

  • Renaming IBOutlets in code without also changing them in IB gives the error "this class is not key value coding-compliant for the key old_outlet_name" (where "old_outlet_name" will be the old name of your outlet.) when you try to load the view controller from the storyboard. Renaming actions )

  • Renaming IBActions in code without also changing them in IB gives an error "unrecognized selector sent to instance big_hex_number" when you try to tap the button (or trigger the action for other controls.)

You should open your storyboard in the main editor and open the source in the assistant editor, then control-drag from your button in IB into your source code just below your other outlet at the top of your class.

Upvotes: 3

toddg
toddg

Reputation: 2906

Your button needs to be an IBOutlet rather than an IBAction. When you ctrl-drag from Interface Builder to your code, make sure you select the 'Outlet' option as shown below:

IB - Code illustration

Then you can change the button title like this:

else {
    startButton.setTitle("Button Title", for: .normal)
    stopFuncs = true
}

Upvotes: 2

Related Questions