swift - 在viewController中调用类函数

时间:2014-10-31 08:40:37

标签: class swift

嗨,我是新的SO所以我希望我做的一切都正确.... :) 我开始几天前的iOS编程,没有我有一个让我疯狂的问题!

以下是FactBook类的代码:

    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()
    })
}
}

所以问题是,我无法调用ViewController中的函数(randomFact和randomImage)。

但是,调用参数#1的错误缺失参数总是出现。 当我将class factbook更改为struct时,我可以调用func,但是self.randomNumberForAll不起作用。

我只想在func randomImage()中使用func randomFact()中的randomnumber ....

谢谢你能不能帮助我:) !!!!

2 个答案:

答案 0 :(得分:0)

我希望这不是盲人试图引导盲人的情况,因为我是新手,但是我最近也参加了这个课程并且正在使用与你类似的代码。似乎像你一样在Factbook中加载Colorwheel应该消除你丢失的参数错误。与色轮不同,它可能必须是变量而不是常量,以便您可以存储和更改randomFactForAll:

让colorWheel = ColorWheel() var factBook = FactBook()

迟到了,希望这会有所帮助

答案 1 :(得分:0)

需要在函数randomFact()和randomImage()中添加关键字“class”,使其成为类函数。

请检查以下question是否存在类似问题。