如何有选择地为每张卡显示解决方案?

时间:2023-02-07 20:12:41

in my app I have a Next button and a Solution button. When you press next a new card comes up (i.e. Image card1), if you press again next another card comes up in a random way. My question is how to display the solution for each card selectively (i.e. for card1 there is the sol1)

在我的应用程序中,我有一个Next按钮和一个Solution按钮。当您按下一个新卡时(即图像卡1),如果再次按下,则会以随机方式出现另一张卡。我的问题是如何有选择地显示每张卡的解决方案(即对于card1有sol1)

@IBAction func nextbutton(sender: AnyObject) {

@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) {

}

}

1 个解决方案

#1


0  

I have attached a complete sample of your problem.The problem with your code was that variables you were creating in the next button are of limited scope and we want the same random no value for the solution card.Now they are beyond the scope of next action and easily accessible in this class just by using the keyword self

我已附上您问题的完整示例。您的代码问题是您在下一个按钮中创建的变量范围有限,我们希望解决方案卡的相同随机值没有。现在它们超出了下一个范围通过使用关键字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.
}

}

#1


0  

I have attached a complete sample of your problem.The problem with your code was that variables you were creating in the next button are of limited scope and we want the same random no value for the solution card.Now they are beyond the scope of next action and easily accessible in this class just by using the keyword self

我已附上您问题的完整示例。您的代码问题是您在下一个按钮中创建的变量范围有限,我们希望解决方案卡的相同随机值没有。现在它们超出了下一个范围通过使用关键字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.
}

}