[英]Swift - Using IBOutlet & IBAction in func?
我在柜台上工作。 60秒过后,我想停止他的工作。 为此,我使用以下代码:
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//calling the wait function
self.callForWait()
}
func game(){
var score : Int = 0
@IBOutlet weak var afficheurScore: UILabel!
@IBAction func boutonPlus(sender: UIButton) {
score = score + 1
afficheurScore.text = "\(score)"
}
}
func callForWait(){
//setting the delay time 60secs.
let delay = 60 * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue()) {
//call the method which have the steps after delay.
self.stepsAfterDelay()
}
}
func stepsAfterDelay(){
//your code after delay takes place here...
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
boutonPlus是一个按钮,当我单击afficheurScore时,一个简单的标签说(number + 1)。
在我的游戏功能中,我遇到了以下错误:
“只能将实例属性声明为IBOutlet / IBAction”
将此代码移出game()函数
@IBOutlet weak var afficheurScore: UILabel!
@IBAction func boutonPlus(sender: UIButton) {
score = score + 1
afficheurScore.text = "\(score)"
}
因此,您可以在类级别使用它,现在它们已在 game()
函数中定义
完整的代码应为:
class FirstViewController: UIViewController {
var score : Int = 0
@IBOutlet weak var afficheurScore: UILabel!
@IBAction func boutonPlus(sender: UIButton) {
score = score + 1
afficheurScore.text = "\(score)"
}
override func viewDidLoad() {
super.viewDidLoad()
//calling the wait function
self.callForWait()
}
func game(){
}
func callForWait(){
//setting the delay time 60secs.
let delay = 60 * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue()) {
//call the method which have the steps after delay.
self.stepsAfterDelay()
}
}
func stepsAfterDelay(){
//your code after delay takes place here...
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.