【发布时间】:2016-05-08 20:42:47
【问题描述】:
所以我正在编写我的第一个应用程序,是的,从头开始,我以前从未做过类似的事情,所以请多多包涵。
我想获取第一个常量的随机值,并使用它来通过视图控制器上的标签确定屏幕上显示的内容,这对某些人来说可能很容易,但我在这里真的很挣扎。我注释掉了我的代码,所以你知道我打算做什么。现在,我知道我可以采用许多不同的方式,例如根本没有标签和图像上的 Photoshop 短语,但不……我想要 CODE!
有什么想法吗?非常感谢大家:3
import UIKit
class ViewController: UIViewController {
let random = Int(arc4random_uniform(11)) //Randomised int values from 0 to 11 (-1)
@IBOutlet weak var text: UILabel!
@IBOutlet weak var phrase: UIButton! //this button should reset the entire process over again
@IBOutlet var imageauthor: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self .imageauthor.image = UIImage(named:"0\(random).jpg") //Viewcontroller displays a random image out of randomised value
self .text.text = ("''") //this should somehow check what the randomised value is and call the Uilabel text bellow it
}
var string1 = ("My fist app has many holes1")
... string2 = ("My fist app has many holes2")
... string3....
【问题讨论】:
-
我不确定你想要什么。您想根据随机值在标签中附加一些文本吗?例如:随机值 = 1,所以标签文本 = string1 ?
-
是的!我的第一个常量 let random = Int(arc4random_uniform(11)) 将从 0-10(11-1=10) 中随机化一个数字,这将确定我的 image.assets 中的图像编号,下一步是显示给定的字符串与图像相关的给定标签。明白了吗?
-
好的,所以@appzYourLife 的回答应该可以完成这项工作。
-
我正在尝试实现他的方法,但仍然......你知道......我真的不知道该怎么做,我有线索但你知道......如果这是然后我需要什么,我会继续尝试和研究语法,直到我得到它的工作,谢谢。
-
你实现了吗?
标签: swift random constants let