2014-10-31 158 views
1

嗨即時通訊新的SO所以我希望我做一切正確.... :) 我開始了幾天前的iOS編程和不,我有一個問題,讓我瘋了!swift - 在viewController中調用類函數

下面是類概況的代碼:

import Foundation 
import UIKit 

public class FactBook { 

    var randomNumberForAll: Int! 

    let factsArray = [ 
     "Ants stretch when they wake up in the morning.", 
     "Ostriches can run faster than horses.", 
     "Olympic gold medals are actually made mostly of silver.", 
     "You are born with 300 bones; by the time you are an adult you will have 206.", 
     "It takes about 8 minutes for light from the Sun to reach Earth.", 
     "Some bamboo plants can grow almost a meter in just one day.", 
     "The state of Florida is bigger than England.", 
     "Some penguins can leap 2-3 meters out of the water.", 
     "On average, it takes 66 days to form a new habit.", 
     "Mammoths still walked the earth when the Great Pyramid was being built.", 
     "You can finish the entire FunFacts iOS app course in the time it takes to set up the emulator for the Android course" 
    ] 

    let imagesArray = [ 
     UIImage(named: "ants.jpg"), 
     UIImage(named: "ostriches.jpg"), 
     UIImage(named: "olymp.jpg"), 
     UIImage(named: "bones.jpg"), 
     UIImage(named: "light.jpg"), 
     UIImage(named: "bamboo.jpg"), 
     UIImage(named: "florida.jpg"), 
     UIImage(named: "penguins.jpg"), 
     UIImage(named: "habit.jpg"), 
     UIImage(named: "mammoth.jpg") 
    ] 

    func randomFact() ->String { 
     //Count 
     var arrayCount = UInt32(factsArray.count) 
     //Random Number from count 
     var unsignedRandomNumber = arc4random_uniform(arrayCount) 
     //Random Number as Int 
     var randomNumber = Int(unsignedRandomNumber) 

     self.randomNumberForAll = randomNumber 

     return factsArray[randomNumber] 
    } 

    func randomImage() -> UIImage { 
     var imageRandom = randomNumberForAll 

     return imagesArray[imageRandom]! 
    } 
} 

這裏是的ViewController:

import UIKit 

class ViewController: UIViewController { 

//Lable, Structs etc.----------------------------------NEU---------------------------- 
@IBOutlet weak var funFactLable: UILabel! 
@IBOutlet weak var funFactButton: UIButton! 
@IBOutlet weak var imageView: UIImageView! 

let colorWheel = ColorWheel() 

var counter = 0 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    //Loads first Label Text 
    funFactLable.text = "An interestig Fun Fact comes with one push!" 
    funFactButton.setTitle("For the first Fun Fact - Push!", forState: UIControlState.Normal) 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

//ACTIONS------------------------------------------------------------------------------------- 
@IBAction func showFunFact() { 
    var randomColor = colorWheel.randomColor() 
    var randomImage = FactBook.randomImage() 
    var randomFact = FactBook.randomFact() 

    counter++ 

    //BackgroundColor change 
    view.backgroundColor = randomColor 
    //Button-TextColor change 
    funFactButton.tintColor = randomColor 
    //Button-Title = Counter + Text 
    funFactButton.setTitle("\(counter). Fun Fact", forState: UIControlState.Normal) 

    //fadeIn fadeOut FactLable 
    self.funFactLable.fadeOut(completion: { 
     (finished: Bool) -> Void in 
     self.funFactLable.text = randomFact 

     self.funFactLable.fadeIn() 
    }) 

    //fadeIn fadeOut ImageView 
    self.imageView.fadeOut(completion: { 
     (finished: Bool) -> Void in 
     self.imageView.image = randomImage 

     self.imageView.fadeIn() 
    }) 
} 
} 

所以問題是,我不能調用函數(randomFact和randomImage)在的ViewController 。

但總是出現調用中參數#1的錯誤缺失參數。 當我將類事實記錄更改爲結構體時,我可以調用func's,但隨後self.randomNumberForAll不起作用。

什麼,我只是想是使用randomnumber從FUNC randomFact()在FUNC randomImage()....

謝謝你,如果u能幫助我:) !!!!

回答

0

我希望這不是一個盲人試圖引領盲人的案例,因爲我是新手,但最近我參加了這門課程,並且與代碼類似地玩弄了代碼。看起來像加載在事實簿中一樣,您的Colorwheel應該消除您缺少的參數錯誤。不像colorwheel雖然,也許它是一個變量,而不是一個恆定的,這樣就可以存儲並改變你的randomFactForAll:

讓colorWheel = ColorWheel() VAR概況=概況()

有點晚,希望這有助於

0

需要將關鍵字「class」添加到函數randomFact()和randomImage()中,以使它們成爲類函數。

請檢查以下question類似的問題。

相關問題