0

我正在使用 Xcode 8 和 Swift 3,并试图创建一个按钮,当我点击它时会改变颜色。

该按钮是黑色的,但是当我单击时应该变成蓝色。该按钮是一个存钱罐的图像。

我继续添加了黑猪和蓝猪的图像,分别带有名称money_offmoney_on

我现在正在编写代码以在单击时将黑色图像变为蓝色。代码本身没有显示错误,但模拟不成功。点击黑色存钱罐没有任何作用。

有谁知道可能会发生什么?

import UIKit

class SecondViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var label: UILabel!
     let step: Float = 1

    @IBOutlet weak var moneybutton: UIButton!

    @IBOutlet var itemTextField: UITextField!

    @IBAction func moneyClicked(_ sender: UIButton) {
        if sender.currentImage === #imageLiteral(resourceName: "money_off"){
            sender.setImage(#imageLiteral(resourceName: "money_on"), for: .normal)}
        else {
            sender.setImage(#imageLiteral(resourceName: "money_off"), for: .normal)
        }
    }
}
4

4 回答 4

1

尝试这个-

class SecondViewController: UIViewController, UITextFieldDelegate {
  @IBOutlet weak var moneybutton: UIButton!

  func viewDidLoad() {
    self.moneybutton.setImage(UIImage(named:"money_on"), for: .selected)
    self.moneybutton.setImage(UIImage(named:"money_off"), for: .normal)
  }

  @IBAction func moneyClicked(_ sender: UIButton) {
   self.moneybutton.isSelected = !self.moneybutton.isSelected
  }
}
于 2017-02-09T08:52:55.930 回答
0

你最好不要依赖 some 的返回值#imageLiteral,而是保持一个状态,就像moneyOff下面的示例代码一样。

(抱歉,没有可用的 Xcode,因此代码可能包含一些语法错误,但您会明白的):

class SecondViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var label: UILabel!
    let step: Float = 1
    var moneyOff = true

    @IBOutlet weak var moneybutton: UIButton!

    @IBOutlet var itemTextField: UITextField!


    @IBAction func moneyClicked(_ sender: UIButton) {
        if moneyOff {
            sender.setImage(#imageLiteral(resourceName: "money_on"), for: .normal)}
        else {
            sender.setImage(#imageLiteral(resourceName: "money_off"), for: .normal)
        }
        moneyOff = !moneyOff // toggle state
    }
}
于 2017-02-09T07:38:00.790 回答
0

下面的代码对我有用:

class ViewController: UIViewController {

     @IBOutlet weak var moneybutton: UIButton!
     func viewDidLoad() 
     {
       moneybutton.setImage(UIImage(named:"money_on"), for: .selected)
       moneybutton.setImage(UIImage(named:"money_off"), for: .normal)
     }

     @IBAction func moneyClicked(_ sender: UIButton)
     {
       if self.btn.isSelected
       {
         self.moneybutton.isSelected = false
       }
       else{
         self.moneybutton.isSelected = true
     }
 }
于 2017-02-09T07:45:52.510 回答
0

我想问题是您在故事板视图控制器之间的连接。

我尝试以编程方式添加按钮并且它可以工作。

import UIKit
import AVFoundation

class ViewController: UIViewController {

    lazy var btn: UIButton = {
        let btn = UIButton()
        btn.translatesAutoresizingMaskIntoConstraints = false
        btn.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleClicked)))
        btn.isUserInteractionEnabled = true
        btn.setImage(UIImage(named: "money_off"), for: .normal)

        return btn
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .green

        view.addSubview(btn)

        btn.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true
        btn.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 50).isActive = true
        btn.widthAnchor.constraint(equalToConstant: 50).isActive = true
        btn.heightAnchor.constraint(equalToConstant: 10).isActive = true

    }

    func handleClicked() {
        if btn.currentImage == UIImage(named: "money_off")  {
            btn.setImage(UIImage(named: "money_on"), for: .normal)
        } else {
            btn.setImage(UIImage(named: "money_off"), for: .normal)
        }
    }
}
于 2017-02-09T07:58:50.463 回答