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

Button Not Working in App

I'm in the process of building the simple iPhone app with Swift and I'm having an issue getting my buttons to work.

I'm doing everything shown in the video from start to finish, but when I run the app in the simulator I can SEE the label and button, but I cannot click the button. Is there a step I'm missing to make the button functional. I tried deleting the button from the interface and then adding it again. It doesn't work as a clickable button even before I add the IBAction method.

Here's my code:

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var funFactLabel: UILabel!

    let factbook = FactBook()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        funFactLabel.text = factbook.randomFact()
    }

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


    @IBAction func newRandomFactButton() {
        funFactLabel.text = factbook.randomFact()
    }

}

3 Answers

This is my code, let me know if you have any questions.

  //
//  ViewController.swift
//  FunFacts
//
//  Created by Iman Mk R on 1/11/15.
//  Copyright (c) 2015 Iman Mk. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var ChangeTheFact: UILabel!
    @IBOutlet weak var buttonColor: UIButton!

    var factBook = FactBook()
    var colorWheel = ColorWheel()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        ChangeTheFact.text = factBook.randomFact()
        view.backgroundColor = colorWheel.randomColor()


    }

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

    @IBAction func ShowNextFact() {
       var randomColor = colorWheel.randomColor()

        UIView.animateWithDuration(4.0, animations: {
            self.ChangeTheFact.text = self.factBook.randomFact()
            self.ChangeTheFact.frame.origin = CGPoint(x: 0, y: 667)
            self.view.backgroundColor = randomColor
            self.buttonColor.tintColor = randomColor

            }, completion: {
                (finished : Bool) in
                UIView.animateWithDuration(4.0){
                self.ChangeTheFact.frame.origin = CGPoint(x: 375, y: 667)
                }
            })



    }

}

This is how the viewController file should look

class ViewController: UIViewController {

    @IBOutlet weak var funFactLabel: UILabel!
    @IBOutlet weak var funFactButton: UIButton!

    let factBook = FactBook()
    let colorWheel = ColorWheel()


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        funFactLabel.text = factBook.randomFact()





    }

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

    @IBAction func showFunFact() {

        var randomColor = colorWheel.randomColor()

        view.backgroundColor = randomColor

        funFactButton.tintColor = randomColor


        funFactLabel.text = factBook.randomFact()
       }
 } 

Thank you both!