如何在Swift 3中设置按钮图像条件

时间:2017-02-10 04:30:43

标签: uibutton swift3 xcode8

我有 3(三)UIButton 两张图片(active.png和notactive.png)

Official
Personal 1
Personal 2

我的目标是当我选择其中一个按钮时,图像设置为active.png而另外两个按钮设置为notactive.png

所选按钮存储在UILabel输出中

感谢有人可以提供帮助。

button.swift

导入UIKit

类ViewController:UIViewController {

// MARK: Outlets

@IBOutlet var officialButton: UIButton!
@IBOutlet var personal1button: UIButton!
@IBOutlet var personal2button: UIButton!
@IBOutlet var outputLbl: UITextField!

// MARK: Properties

var output: String!
var buttons: [UIButton] {
    return [officialButton, personal1button, personal2button]
}

// MARK: Lifecycle

override func viewDidLoad() {

    super.viewDidLoad()

    buttons.forEach {
        $0.addTarget(self, action: #selector(didTouchUpInside(button:)), for: .touchUpInside)
        $0.setImage(UIImage(named: "notactive"), for: .normal)
        $0.setImage(UIImage(named: "active"), for: .selected)
    }
}

// MARK: Methods

func didTouchUpInside(button: UIButton) {

    buttons.forEach { $0.isSelected = false }
    button.isSelected = !button.isSelected

    outputLbl.text = nil

    if button.isSelected {
        outputLbl.text = button.currentTitle
    }
    else if officialButton.isSelected {
        outputLbl.text = "Official"
    }
    else if personal1Button.isSelected {
        outputLbl.text = "P1"
    }
    else if personal2Button.isSelected {
        outputLbl.text = "P2"
    }
}

}

感谢。

2 个答案:

答案 0 :(得分:2)

import UIKit

class ViewController: UIViewController {

    // MARK: Outlets

    @IBOutlet var officialButton: UIButton!
    @IBOutlet var personal1button: UIButton!
    @IBOutlet var personal2button: UIButton!
    @IBOutlet var outputLbl: UITextField!

    // MARK: Properties

    var output: String!
    var buttons: [UIButton] {
        return [officialButton, personal1button, personal2button]
    }

    // MARK: Lifecycle

    override func viewDidLoad() {

        super.viewDidLoad()

        buttons.forEach {
            $0.addTarget(self, action: #selector(didTouchUpInside(button:)), for: .touchUpInside)
            $0.setImage(UIImage(named: "notactive"), for: .normal)
            $0.setImage(UIImage(named: "active"), for: .selected)
        }
    }

    // MARK: Methods

    func didTouchUpInside(button: UIButton) {

        buttons.forEach { $0.isSelected = false }
        button.isSelected = !button.isSelected

        outputLbl.text = nil

        if button.isSelected {
            outputLbl.text = button.currentTitle
        }
    }
}

答案 1 :(得分:1)

class ViewController: UIViewController {

    @IBOutlet var officialButton: UIButton!
    @IBOutlet var personal1button: UIButton!
    @IBOutlet var personal2button: UIButton!

    @IBOutlet var outputLbl: UITextField!

    var output: String!

    override func viewDidLoad() {
        super.viewDidLoad()

         officialButton.addTarget(self, action: #selector(pressButton(button:)), for: .touchUpInside)
         personal1button.addTarget(self, action: #selector(pressButton(button:)), for: .touchUpInside)
         personal2button.addTarget(self, action: #selector(pressButton(button:)), for: .touchUpInside
    }

func pressButton(button: UIButton) {
        officialButton.setImage(UIImage(named: "notactive"), for: .selected)
        personal1button.setImage(UIImage(named: "notactive"), for: .selected)
        personal2button.setImage(UIImage(named: "notactive"), for: .selected)
       button.setImage(UIImage(named: "active"), for: .selected)
       outputLbl.text = button.titlelable
}
}