0

I want to add an image to a button, and when it is clicked, i want it to change. I'm very new to Swift so I know very little.

I have it like this:

//
//  ViewController.swift
//  Tip Calculator
//
//  Created by ios4 on 10/16/15.
//  Copyright (c) 2015 ios4. All rights reserved.
//

import UIKit

class ViewController: UIViewController
{
    @IBOutlet weak var tipAmount: UILabel!
    @IBOutlet weak var userBillTextField: UITextField!
    @IBOutlet weak var tenOutlet: UIButton!



    override func viewDidLoad()
    {

        tipAmount.text = " "
      // var billDouble =  (userBillTextField.text as NSString).doubleValue


        super.viewDidLoad()

    }

    @IBAction func calculateButton(sender: UIButton)
    {
       var billDouble =  NSString(string: userBillTextField.text).doubleValue
        tipAmount.text = String( stringInterpolationSegment: billDouble * 0.15)
    }

    @IBAction func tenButton(sender: UIButton)
    {



}

and I want the outlet called ten outlet to change to a different image called 10_selected_image from 10_unselected_image.

Any help?

Here are my instructions for this : https://s3.amazonaws.com/mmhighschool/MasterDocs/TipCalculator/TipCalculatorAppDirections.pdf

I'm currently working on stretch 3

2 Answers2

1

You should be able to use set image for control state in your button action

button.setImage(image: UIImage(named: <image name>), forState: UIControlState.Normal)

If you need more info about how to make a "toggle button" this thread has a good answer to that How to use a UIButton as a toggle switch

Community
  • 1
  • 1
Moriya
  • 7,750
  • 3
  • 35
  • 53
0

For me in Swift 5 is working like:

boton1.setBackgroundImage(UIImage(named: "image Name"), for: UIControl.State.normal)
Mickael Maison
  • 25,067
  • 7
  • 71
  • 68