所以我正在编写我的第一个应用程序,是的,从头开始,我之前从未做过类似的事情,所以请耐心等待。
我想取第一个常量的随机值并使用它来通过视图控制器上的标签来确定屏幕上显示的内容,对于某些人来说这可能很容易,但我真的很难在这里挣扎。我注释掉了我的代码,所以你知道我打算做什么。现在,我知道我可以采用许多不同的方式,例如根本没有标签和图片上的photoshop短语,但是......我想要代码!
有什么想法吗?非常感谢你们:3< 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....
答案 0 :(得分:0)
sentences
)。0
和sentences.count
之间的随机值(不包括在内)。sentences
数组中选择一个值。像这样:
let sentences = ["My fist app has many holes1", "My fist app has many holes2", "My fist app has many holes3"];
let random = Int(arc4random_uniform(UInt32(sentences.count)))
let randomSentence = sentences[random]
答案 1 :(得分:0)
具体来说,它应该是这样的:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var imageauthor: UIImageView!
@IBOutlet weak var text: UILabel!
// 1
@IBAction func showRandom(sender: UIButton) {
showRandom()
}
// 2
let arrayOfStrings: [String] = ["String of image 0", "String of image 1", "String of image 2", "String of image 3", "String of image 4", "String of image 5", "String of image 6", "String of image 7", "String of image 8", "String of image 9"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//3
showRandom()
}
//4
func showRandom() {
let random = Int(arc4random_uniform(UInt32(arrayOfStrings.count)))
self.imageauthor.image = UIImage(named:"0\(random).jpg")
self.text.text = (arrayOfStrings[random])
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
// 1 在您的按钮上创建连接Action
:它与Outlet
的流程相同,但选择Connection : Action
和Type : UIButton
代替。并在点击时执行随机功能。 See documentation
// 2 声明一个包含所有句子的数组。尊重订单。
// 3 在viewDidLoad上执行随机函数
// 4 创建将随机图像和相应文本放入imageView和标签的函数showRandom()
。