在 Swift 中单击后如何更改 UIButton 图像?

How to change UIButton image after clicking in Swift?(在 Swift 中单击后如何更改 UIButton 图像?)
本文介绍了在 Swift 中单击后如何更改 UIButton 图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编码开发的新手,所以这应该是一个非常菜鸟的问题.但是如果我的 if 语句执行,我正在尝试将我的 UIButton 的图像更改为存储在 image.casset "duellogo" 中的图像.但我不确定如何更改 UIButton Image;我只知道如何更改 UIImageView 图像.

I'm new to coding development so this should be a pretty noob question. But I'm trying to change the image of my UIButton to an image stored in image.casset "duellogo" if my if statement executes. But I am unsure how to change UIButton Image; I only know how to change UIImageView Image.

import UIKit

class ViewController: UIViewController 
{

    @IBOutlet weak var firstCardImageView: UIImageView!
    @IBOutlet weak var secondCardImageView: UIImageView!
    @IBOutlet weak var playRoundButton: UIButton!
    @IBOutlet weak var backgroundImageView: UIImageView!

    @IBOutlet weak var Player1Score: UILabel!
    @IBOutlet weak var Player2Score: UILabel!

    //declaring index score, 0, to player 1 and player 2
    var player1Total=0
    var player2Total=0

    var cardNamesArray:[String] = ["ace", "card2", "card3", "card4", "card5", "card6", "card7", "card8", "card9", "card10", "jack", "queen", "king"]

    override func viewDidLoad() 
    {
        super.viewDidLoad()
        // 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.
    }

    @IBAction func playRoundTapped(sender: UIButton) 
    {
        // create variable to make a random number from 1-13
        var firstRandomNumber:Int = Int(arc4random_uniform(13))
        var firstCardString:String = self.cardNamesArray[firstRandomNumber]
        self.firstCardImageView.image = UIImage(named: firstCardString)

        // create variable to make a random number from 1-13 for second card
        var secondRandomNumber = Int(arc4random_uniform(13))
        var secondCardString:String = self.cardNamesArray[secondRandomNumber]
        self.secondCardImageView.image = UIImage(named: secondCardString)


        // Determine the higher card
        if firstRandomNumber > secondRandomNumber{
            //TODO: first card is lager
            player1Total+=1
            self.Player1Score.text = String(player1Total)
        }else if(firstRandomNumber == secondRandomNumber){
            // ERROR: TRYING TO CHANGE BUTTON IMAGE
            self.playRoundButton.setImage("duellogo", forState: UIControlState.Normal)
            //    ^^ ERROR
        }else{
            //TODO: second card is larger
            player2Total+=1
            self.Player2Score.text = String(player2Total)
        }
    }
}

推荐答案

试试这个代码

let image = UIImage(named: "duellogo.png") as UIImage!
self.playRoundButton.setImage(image, forState: .Normal)

这篇关于在 Swift 中单击后如何更改 UIButton 图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)