2016-05-08 26 views
0

我非常新的Xcode和刷卡,非常感謝任何幫助。我爲屏幕上半部分的單詞列表創建了一個隨機生成器(頂部標籤,這包含在代碼中)。我如何調整這段代碼,以便我可以在屏幕的下半部分使用不同的單詞列表(底部標籤不是指向任何代碼)?但是對於相同的點擊按鈕來隨機化這兩個單詞列表? The layout of the screen如何在同一視圖控制器上的2個不同視圖上實現隨機生成器?

進口的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) 


} 

}

+0

我有點困惑,因爲只有一個代碼標籤,但你有兩個佈局。 – penatheboss

+0

對不起,底部標籤尚未連接到代碼。 –

+0

一旦你這樣做,你能[編輯你的問題](http://stackoverflow.com/posts/37100997/edit)嗎? – penatheboss

回答

1

使用擴展它是全球性的。你可以使用shuffle數組並打印報價。你可以聲明數組的全局引用,所以你可以在任何視圖控制器中訪問它。

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 

OUTPUT

洗牌擴展不被我寫的。 積分內特庫克https://stackoverflow.com/a/24029847/2803960

+0

謝謝,我只是想解決它。 –

相關問題