首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在同一视图控制器上的两个不同视图上实现随机生成器?

如何在同一视图控制器上的两个不同视图上实现随机生成器?
EN

Stack Overflow用户
提问于 2016-05-08 14:38:22
回答 1查看 71关注 0票数 0

我非常新的Xcode和滑动,任何帮助都是非常感谢的。我已经为屏幕上半部分的单词列表创建了一个随机生成器(顶部标签,这包括在代码中)。如何调整这段代码,以便在屏幕的下半部分使用不同的单词列表(下面的标签不被任何代码所吸引)?但是对于相同的单击按钮,将两个单词列表随机化??屏幕的布局

导入UIKit

类ViewController: UIViewController { //放置标签和按钮

代码语言:javascript
复制
@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)


}

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-08 14:47:38

使用扩展它是全局的。您可以使用洗牌数组并打印引号。您可以将引号数组声明为全局数组,以便可以在任何视图控制器中访问它。

代码语言:javascript
复制
   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 Cookhttps://stackoverflow.com/a/24029847/2803960

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37100997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档