我非常新的Xcode和滑动,任何帮助都是非常感谢的。我已经为屏幕上半部分的单词列表创建了一个随机生成器(顶部标签,这包括在代码中)。如何调整这段代码,以便在屏幕的下半部分使用不同的单词列表(下面的标签不被任何代码所吸引)?但是对于相同的单击按钮,将两个单词列表随机化??屏幕的布局
导入UIKit
类ViewController: UIViewController { //放置标签和按钮
@IBOutlet var InspirationalThought: UILabel!
@IBOutlet var Click: UIButton!
//what the label is going to show
var quotes = ["Geometrics", "Vectors", "Celebration", "Triangle", "Landscapes", "Seasons", "Snow", "Rain", "Sunrays", "Stencils", "Paint", "Graphics", "Graffiti", "Sports","Fashion",
"Ancient Greek", "Philosophers", "Fairy Tales", "Fantasy", "Clouds", "Mystery", "Time Clocks", "Canvas", "Tie-dye", "Glitter", "Dessert", "Desert", "Energy", "Astrology", "Solar Systems", "Sea", "Beach", "Sphere", "Roots", "Lights", "Darks", "Fire", "Air",
"Aperture", "Long exposure", "Portraits", "World", "Travel", "Architecture", "Freedom", "Old", "New", "Urban", "Lenses", "Fisheye", "Chords", "Music Notes", "Spices", "Herbs", "Natural", "Marbles", "Wood", "Trees", "Forests", "Interior",
"Mammals", "Reptiles", "Ocean", "Birds", "Photography", "Exposure", "Opaque", "Translucent", "Freestyle", "Spots", "Stripes", "Zig Zag", "Spiral", "Glass", "Feathers", "Calm", "Bulb", "Heat", "Cold", "Stitches", "Views", "Birds", "Sunset", "Earth"]
var whichQuotestoChoose = 0
// what the button is going to show
var ButtonText = ["Inspiration", "Tell me more", "more inspirational"]
var whichButtonTexttoChoose = 0
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func ClickButtonClicked(sender: UIButton) {
chooseAQuote()
chooseTextonButton()
}
func chooseAQuote(){
showTheQuote()
whichQuotestoChoose = Int(arc4random_uniform (84))
}
func showTheQuote(){
InspirationalThought.text = "\(quotes[whichQuotestoChoose])"
}
func chooseTextonButton(){
Click.setTitle("\(ButtonText[whichButtonTexttoChoose])", forState: UIControlState.Normal)
}}
发布于 2016-05-08 14:47:38
使用扩展它是全局的。您可以使用洗牌数组并打印引号。您可以将引号数组声明为全局数组,以便可以在任何视图控制器中访问它。
var quotes = ["Geometrics", "Vectors", "Celebration", "Triangle", "Landscapes", "Seasons", "Snow", "Rain", "Sunrays", "Stencils", "Paint", "Graphics", "Graffiti", "Sports","Fashion",
"Ancient Greek", "Philosophers", "Fairy Tales", "Fantasy", "Clouds", "Mystery", "Time Clocks", "Canvas", "Tie-dye", "Glitter", "Dessert", "Desert", "Energy", "Astrology", "Solar Systems", "Sea", "Beach", "Sphere", "Roots", "Lights", "Darks", "Fire", "Air",
"Aperture", "Long exposure", "Portraits", "World", "Travel", "Architecture", "Freedom", "Old", "New", "Urban", "Lenses", "Fisheye", "Chords", "Music Notes", "Spices", "Herbs", "Natural", "Marbles", "Wood", "Trees", "Forests", "Interior",
"Mammals", "Reptiles", "Ocean", "Birds", "Photography", "Exposure", "Opaque", "Translucent", "Freestyle", "Spots", "Stripes", "Zig Zag", "Spiral", "Glass", "Feathers", "Calm", "Bulb", "Heat", "Cold", "Stitches", "Views", "Birds", "Sunset", "Earth"]
extension CollectionType {
/// Return a copy of `self` with its elements shuffled
func shuffle() -> [Generator.Element] {
var list = Array(self)
list.shuffleInPlace()
return list
}
}
extension MutableCollectionType where Index == Int {
/// Shuffle the elements of `self` in-place.
mutating func shuffleInPlace() {
// empty and single-element collections don't shuffle
if count < 2 { return }
for i in 0..<count - 1 {
let j = Int(arc4random_uniform(UInt32(count - i))) + i
guard i != j else { continue }
swap(&self[i], &self[j])
}
}
}
quotes = quotes.shuffle()
print(quotes[0]) // Print random quote

洗牌分机不是我写的。Nate Cook:https://stackoverflow.com/a/24029847/2803960
https://stackoverflow.com/questions/37100997
复制相似问题