2015-03-31 42 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

相关问题