在我的应用程序中,我有一个Next按钮和一个Solution按钮。当您按下一个新卡时(即图像卡1),如果再次按下,则另一张卡会以随机方式出现。我的问题是如何有选择地显示每张卡的解决方案(即对于card1有sol1)
@IBAction func nextbutton(sender:AnyObject){
//Randomize a number for the first imageview
var firstRandomNumber = arc4random_uniform(2) + 1
//Construct a string with the random number
var firstCardString:String = String(format: "card%i", firstRandomNumber)
// Set the first card image view to the asset corresponding to the randomized number
self.Image1.image = UIImage(named: firstCardString)
}
@IBAction func solutionbutton(sender: AnyObject) {
}
答案 0 :(得分:0)
我附上了你的问题的完整样本。你的代码的问题是你在下一个按钮中创建的变量范围有限,我们希望解决方案卡中没有相同的随机值。现在它们超出了下一步操作的范围,只需使用关键字 self
即可在此课程中轻松访问import UIKit
class YourViewController : UIViewController {
var randomNumber:Int = 0
var cardString:String = ""
var solutionString:String = ""
@IBAction func nextbutton(sender: AnyObject) {
//Randomize a number for the first imageview
self.randomNumber = arc4random_uniform(2) + 1
//Construct a string with the random number
self.cardString = String(format: "card%i", self.randomNumber)
self.solutionString = String(format: "sol%i", self.randomNumber)
// Set the first card image view to the asset corresponding to the randomized number
self.Image1.image = UIImage(named: self.cardString)
}
@IBAction func solutionbutton(sender: AnyObject) {
self.Image1.image = UIImage(named: self.solutionString)
}
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.
}
}