2014-10-31 154 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类似的问题。

相关问题