这是我的代码。我想在三个按钮中显示三个随机食物图像。我检查语法,看起来也不错。我不确定图像。我在图像集中有1x,2x和3x图像。
import UIKit
import GameplayKit
class ViewController: UIViewController {
@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button3: UIButton!
var foodItems = [String]()
override func viewDidLoad() {
super.viewDidLoad()
//Add food items into Array
foodItems.append("Apple")
foodItems.append("Banana")
foodItems.append("Bhindi")
foodItems.append("Bread")
foodItems.append("CornFlakes")
foodItems.append("Dal Dhokli")
foodItems.append("Egg")
foodItems.append("Milk")
foodItems.append("Nan")
foodItems.append("Roti")
// Add border to button(default color is black)
button1.layer.borderWidth = 1
button2.layer.borderWidth = 1
button3.layer.borderWidth = 1
// change border color to gray
button1.layer.borderColor = UIColor.gray.cgColor
button2.layer.borderColor = UIColor.gray.cgColor
button3.layer.borderColor = UIColor.gray.cgColor
// Do any additional setup after loading the view, typically from a nib.
askQuestion()
}
// ask question to identify foodItem
func askQuestion() {
foodItems = GKRandomSource.sharedRandom().arrayByShufflingObjects(in: foodItems) as! [String]
//set image to button 1, 2 and 3
button1.setImage(UIImage(named: foodItems[0]), for: .normal)
button2.setImage(UIImage(named: foodItems[1]), for: .normal)
button3.setImage(UIImage(named: foodItems[2]), for: .normal)
}
这是模拟器视图。我只能看到蓝色方块。我在图像集中有1x,2x和3x图像。