嗨,我是 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]!
}
}
这是 View Controller :
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 的错误缺少参数。 当我将类概况更改为结构时,我可以调用函数,但 self.randomNumberForAll 不起作用。
我只想在 func randomImage() 中使用 func randomFact() 中的随机数....
如果你能帮助我,谢谢 :) !!!!
最佳答案
我希望这不是盲人试图引导盲人的情况,因为我是 swift 的新手,但我最近才上这门类(class),并且和你一样在玩代码。似乎像 Colorwheel 一样加载 Factbook 应该可以消除丢失的参数错误。与色轮不同的是,它可能必须是一个变量而不是一个常量,这样您就可以存储和更改您的 randomFactForAll:
让 colorWheel = ColorWheel() var factBook = FactBook()
有点晚了,希望对你有帮助
关于class - swift - 在 viewController 中调用类函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26670457/