使用按钮时图像不显示
Images are not showing up when using a button
当我点击运行应用程序时,当我点击指定按钮时应用程序没有放置图像。 (我对 Swift 和 Xcode 比较陌生,所以请多多包涵)
我将删除 UIImageViews 并改用按钮。如果我需要更多帮助,我会通知您。谢谢。
class ViewController: UIViewController {
@IBOutlet var TicTacImg1: UIImageView!
@IBOutlet var TicTacImg2: UIImageView!
@IBOutlet var TicTacImg3: UIImageView!
@IBOutlet var TicTacImg4: UIImageView!
@IBOutlet var TicTacImg5: UIImageView!
@IBOutlet var TicTacImg6: UIImageView!
@IBOutlet var TicTacImg7: UIImageView!
@IBOutlet var TicTacImg8: UIImageView!
@IBOutlet var TicTacImg9: UIImageView!
@IBOutlet var TicTacButton1: UIButton!
@IBOutlet var TicTacButton2: UIButton!
@IBOutlet var TicTacButton3: UIButton!
@IBOutlet var TicTacButton4: UIButton!
@IBOutlet var TicTacButton5: UIButton!
@IBOutlet var TicTacButton6: UIButton!
@IBOutlet var TicTacButton7: UIButton!
@IBOutlet var TicTacButton8: UIButton!
@IBOutlet var TicTacButton9: UIButton!
@IBOutlet var ResetButton: UIButton!
@IBOutlet var UserMessage: UILabel!
var plays = Dictionary <Int, Int> ()
@IBAction func UIButtonClicked(sender:UIButton) {
UserMessage.hidden = true
if plays[sender.tag] == nil && !aiDeciding && !done {
setImageForSpot(sender.tag, player:1)
}
}
func setImageForSpot (spot:Int, player:Int) {
var playerMark = player == 1 ? "x" : "o"
plays[spot] = player
switch spot {
case 1:
TicTacImg1.image = UIImage(named: playerMark)
case 2:
TicTacImg2.image = UIImage(named: playerMark)
case 3:
TicTacImg3.image = UIImage(named: playerMark)
case 4:
TicTacImg4.image = UIImage(named: playerMark)
case 5:
TicTacImg5.image = UIImage(named: playerMark)
case 6:
TicTacImg6.image = UIImage(named: playerMark)
case 7:
TicTacImg7.image = UIImage(named: playerMark)
case 8:
TicTacImg8.image = UIImage(named: playerMark)
case 9:
TicTacImg9.image = UIImage(named: playerMark)
default:
TicTacImg5.image = UIImage(named: playerMark)
}
}
您的 UIButtonClicked 函数需要如下所示...
@IBAction func UIButtonClicked(sender:UIButton) {
ticTacLabel.hidden = true
if plays[sender.tag] == nil && !aiDeciding && !done {
setImageForSpot(sender.tag, player:1)
}
希望对您有所帮助!
当我点击运行应用程序时,当我点击指定按钮时应用程序没有放置图像。 (我对 Swift 和 Xcode 比较陌生,所以请多多包涵)
我将删除 UIImageViews 并改用按钮。如果我需要更多帮助,我会通知您。谢谢。
class ViewController: UIViewController {
@IBOutlet var TicTacImg1: UIImageView!
@IBOutlet var TicTacImg2: UIImageView!
@IBOutlet var TicTacImg3: UIImageView!
@IBOutlet var TicTacImg4: UIImageView!
@IBOutlet var TicTacImg5: UIImageView!
@IBOutlet var TicTacImg6: UIImageView!
@IBOutlet var TicTacImg7: UIImageView!
@IBOutlet var TicTacImg8: UIImageView!
@IBOutlet var TicTacImg9: UIImageView!
@IBOutlet var TicTacButton1: UIButton!
@IBOutlet var TicTacButton2: UIButton!
@IBOutlet var TicTacButton3: UIButton!
@IBOutlet var TicTacButton4: UIButton!
@IBOutlet var TicTacButton5: UIButton!
@IBOutlet var TicTacButton6: UIButton!
@IBOutlet var TicTacButton7: UIButton!
@IBOutlet var TicTacButton8: UIButton!
@IBOutlet var TicTacButton9: UIButton!
@IBOutlet var ResetButton: UIButton!
@IBOutlet var UserMessage: UILabel!
var plays = Dictionary <Int, Int> ()
@IBAction func UIButtonClicked(sender:UIButton) {
UserMessage.hidden = true
if plays[sender.tag] == nil && !aiDeciding && !done {
setImageForSpot(sender.tag, player:1)
}
}
func setImageForSpot (spot:Int, player:Int) {
var playerMark = player == 1 ? "x" : "o"
plays[spot] = player
switch spot {
case 1:
TicTacImg1.image = UIImage(named: playerMark)
case 2:
TicTacImg2.image = UIImage(named: playerMark)
case 3:
TicTacImg3.image = UIImage(named: playerMark)
case 4:
TicTacImg4.image = UIImage(named: playerMark)
case 5:
TicTacImg5.image = UIImage(named: playerMark)
case 6:
TicTacImg6.image = UIImage(named: playerMark)
case 7:
TicTacImg7.image = UIImage(named: playerMark)
case 8:
TicTacImg8.image = UIImage(named: playerMark)
case 9:
TicTacImg9.image = UIImage(named: playerMark)
default:
TicTacImg5.image = UIImage(named: playerMark)
}
}
您的 UIButtonClicked 函数需要如下所示...
@IBAction func UIButtonClicked(sender:UIButton) {
ticTacLabel.hidden = true
if plays[sender.tag] == nil && !aiDeciding && !done {
setImageForSpot(sender.tag, player:1)
}
希望对您有所帮助!