2015-03-31 86 views
1

我在櫃檯工作。我想在60秒過後停止工作。對於這一點,我用這個代碼:Swift - 在函數中使用IBOutlet和IBAction?

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,一個簡單的標籤說(數+ 1)。

在我的遊戲功能,我有這個錯誤:

「只有實例屬性可以聲明IBOutlet中/ IBAction爲」

回答

1

移動這個代碼出局()函數

@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. 
    } 
} 
+0

感謝您的回覆,「只有實例屬性可以聲明IBOutlet中/ IBAction爲」已經消失,但我有這樣的問題:得分在遊戲功能聲明,IBAction爲不有訪問權。 – 2015-03-31 07:39:22

+0

哪個問題?你只說出口/行動問題... – 2015-03-31 07:40:34

+0

是的,真的,謝謝你:)。 – 2015-03-31 07:43:02

相關問題