swift 如何有选择地显示每张卡的解决方案?
Posted
技术标签:
【中文标题】swift 如何有选择地显示每张卡的解决方案?【英文标题】:swift how to display solution for each card selectively? 【发布时间】:2015-02-01 10:48:25 【问题描述】:在我的应用程序中,我有一个下一步按钮和一个解决方案按钮。当您按下下一张时,会出现一张新卡(即图像卡 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)
【问题讨论】:
【参考方案1】:我附上了您问题的完整示例。您的代码的问题是您在下一个按钮中创建的变量范围有限,我们希望解决方案卡具有相同的随机无值。现在它们超出了只需使用关键字 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.
【讨论】:
以上是关于swift 如何有选择地显示每张卡的解决方案?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 iOS8.1 上使用 Swift 按顺序拍摄多张照片(每张延迟 1 秒)?